In: Computer Science
This is just a very basic program/problem in Java I'm looking for some second opinions on.
1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive).
2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked.
3. Take a year input from the user (should be between 1500 and 2019)
4. If the input year is a leap year, print that year is a leap year; otherwise, print that the year is not a leap year.
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new java program with name "LeapYear.java" is created, which contains following code.
LeapYear.java :
import java.util.*;//import package
//Java class
public class LeapYear {
// entry point , main() method
public static void main(String[] args) {
// creating object of Scanner
class
Scanner sc = new
Scanner(System.in);
// asking user to enter year
System.out.print("Enter year
between 1500 and 2019 : ");
int year = sc.nextInt();// reading
year
// checking year
if (year > 1500 && year
< 2019) {
// checking leap
year
if (((year % 4
== 0) && (year % 100 != 0)) || (year % 400 == 0)) {
// print message when year is leap year
System.out.println(year + " is leap
year.");
} else {
// print message when year is not leap
year
System.out.println(year + " is not leap
year.");
}
} else {
// print message
if invalid year is entered
System.out.println("Year cannot be checked.Enter year between 1500
and 2019");
}
}
}
======================================================
Output : Compile and Run LeapYear.java to get the screen as shown below
Screen 1 :LeapYear.java, Screen when year is not in the range
Screen 2 :LeapYear.java, Screen showing when year is leap
Screen 3 :LeapYear.java, Screen showing when year is not leap
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.