Java program to check for leap year
sample program:
import java.io.*; import java.util.Scanner; public class TestClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); boolean leap = false; if(year % 4 == 0) { if( year % 100 == 0) { // year is divisible by 400, hence the year is a leap year if ( year % 400 == 0) leap = true; else leap = false; } else leap = true; } else leap = false; if(leap) System.out.println(year + " is a leap year"); else System.out.println(year + " is not a leap year"); } }
Output:
1994 | 1994 is not a leap year |
2000 | 2000 is a leap year |
2012 | 2012 is a leap year |
2083 | 2083 is not a leap year |
Tags JAVA languages
Subscribe Our Newsletter
import java.util.Scanner;
ReplyDeleteimport java.io.*;
public class TestClass
{
public static void main(String[] args)
{
int year;
Scanner sc=new Scanner(System.in);
year=sc.nextInt();
if(year%4==0) System.out.println(year+" is a leap year");
else System.out.println(year+" is not a leap year");
}
}