Ashley P.

asked • 10/03/19

Java Programming

Hi, I'm new to Java. I've been working on this question, wrote the code and checked the results with some examples and it seems to be doing fine. I would be glad if any of the tutors can check the code for me.

Also, please do let me know if there's a more efficient way for writing the code.

Many thanks!


Question :


Write a Java program that replies either Leap Year or Not a Leap Year, when a year is given as keyboard input.

(i) A Year is a Leap Year if the year can be divided by 4 but not by 100

(eg: 1976).

(ii) A year that can be divided by both 4 and 100 is Leap Year if it can also

be divided by 400.



My code :

package exams;


import java.util.*;


public class Exams {


public static void main(String[] args) {


Scanner input = new Scanner(System.in);


int p, q, r, s;


System.out.print("Enter Year");

p = input.nextInt();


if(p%4==0){


if(p%100==0){


if(p%400==0){


System.out.println("Leap Year");

}


}


else if(p%100!=0){

System.out.println("Not a Leap Year");


}


}


}


}

1 Expert Answer

By:

Suzanne O. answered • 10/03/19

International Experience and Multiple State Certifications

Ashley P.

Thanks a lot for your response. The explanation was very clear and I was able to correct the errors in my code
Report

10/07/19

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.