Write a JAVA Program for Check Year is leap - Free Web Development || UI Development || React JS || Angular

Latest

Free Web Development ||Free UI Development || React JS || HTML5 || CSS3 || JavaScript || Angular Learn Web Development || Frontend Developer || Full Stack Developer

1 / 3
Welcome to Shravan Ghanchi
2 / 3
3 / 3

Monday, December 14, 2020

Write a JAVA Program for Check Year is leap

Java Program for check year is a leap

Write a Java program to check whether the input year is a leap or not. Before we see the java program first we understand the leap year program logic in java.



(1) If the year is evenly divisible by 4,go to step2,otherwise go to step5
(2)if the year is evenly divisible by 100,go to step 3 otherwise go to step4.
(3)if the year is evenly divisbly by 400 then go to step 4,otherwise,go to step5.
(4)The year is leap year it has 366 days.
(5)The Year is Not Leap Year it has 365 days.
Example: program to check whether the input year is leap or not.
We are using scanner class to get the input from user and then we are using If-else statement to write the logic to check the year is leap or not.
Process or Algorithm:
(1) We take the year integer variable in java.
(2)Assign a value to the integer variable.
(3)Check if the year is divisible by 4 but not 100 display "Year is leap"
(4)Check if the year is divisible by 4 years is a leap.
(5)Otherwise display not leap year.

Code:

package com;

 

import java.util.Scanner;

 

public class LeapYear {

 

    public static void main(String[] args) {

       int year;

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter any Year");

       year=sc.nextInt();

       sc.close();

       boolean LeapYear=false;

       if(year%4==0)

       {

           if(year%100==0)

           {

               if(year%400==0)

              

                  LeapYear=true;

              

               else

                  LeapYear=false;

           }

               else

                  LeapYear=true;

               }

               else {

                  LeapYear=false;

                  }

           if(LeapYear==true)

      System.out.println(year +"is a leap year");

       else

   System.out.println(year +"is not a leap year");

 }

 



No comments:

Post a Comment

Snow
Forest
Mountains
Snow
Forest
Mountains