The java program given below is a program that is able to check a year whether it is a leap year or not. This java program is written simply but work effectively. I have used normal if...else condition to check a year.
After running the program, a message will be displayed to enter a year. After entering the year it will be stored in an integer type variable year.
Later we have to divide the year by 100 and need to check if there is any remainder or not. If there is no any remainder after dividing by 100, then we need to divide it by 400 again. If there is no any remainder even after dividing by 400, then we can declare it as a leap year.
import java.util.Scanner; public class LeapYear{ public static void main(String [] args){ //declaring integer type variable int year; //displaying message to input a year System.out.print("Enter a year: "); //getting input from keyboard Scanner in = new Scanner(System.in); year = in.nextInt(); //checking whether it is leap year or not if(year%100==0 || year%4==0 && year%400==0){ System.out.println(year + " is a Leap Year"); } else { System.out.println(year + " is not a Leap Year"); } } }
No comments:
Post a Comment