Java code:
import java.util.Scanner
class Leapyear
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in)
int year;
System.out.println("Enter any year : ");
year = sc.nextInt();
if(year%4 == 0)
{
System.out.println("This year is a Leap year.");
}
else
{
System.out.println("This year is not a Leap year.");
}
}
}
Output-1
Enter any year : 2022This year is not a Leap year.
Output-2
Enter any year : 2024This year is a Leap year.
Explanation :
Step:1First create a class. In this program create one class and the class name is Leapyear .
Step:2
Under the class, create a main function.
Step:3
Under the main function, create an object of the scanner class and the object name is sc.
Step:4
Declare one integer variable, that name is year. This variable store the user input using the nextInt() method of the scanner class.
Step:5
Use conditional statement(if/else) to check year is leap year or not. If (year%4 == 0) So, that condition is true and this year is leap year. else condition is false so, year is not a leap year. Why use year % 4 because every 4 year one year is leap year. In output:1 , user input year is 2022. (2022 % 4 = 2) So, that result is false and output is not a leap year. In output:2 , user input year is 2024. (2024 % 4 = 0) So, that result is true and output is leap year.
Thank You.
0 Comments