dark_mode

How to Check if a year is Leap year Using Java Programming language


    Table of Contents   expand_more
  1. How to Check if a year is Leap Year
  2. Question Picked ✔️ ✔️: Write a program to print leap years from 2000 to 2021

How to Check if a year is Leap Year



Example

Example


Code
                                                              
//By GadTool.blogspot.com
import java.util.Scanner;
class
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int GadTool;
System.out.println("Enter the year:");
GadTool=sc.nextInt();
if(GadTool%4==0){
System.out.println("LEAP YEAR");
}else {
System.out.println("Year is not a leap year");
}
}
}

➡️ Enter the name of the class in which you are typing or pasting the above code.


Question Picked ✔️ ✔️: Write a program to print leap years from 2000 to 2021



Java Programming be like
Output


Code
                                                              
//By GadTool.blogspot.com
class
{
public static void main()
{
int GadTool;
System.out.println("Leap Year list from 2000 to 2021:");
for(GadTool=2000;GadTool<=2021;GadTool++){
if(GadTool%4==0){
System.out.println(GadTool);
}
}
}

➡️ Enter the name of the class in which you are typing or pasting the above code.



Explanation

First of all, we have initialized an integer named  GadTool  to carry the years from 2000 to 2021 as its value. Then we printed the heading of the program.
Subsequently,   for   loop is added so that it could run a check on every value of the variable  GadTool .

Java Program   for   loop in a Nutshell⇾
1. Initialization-: We assigned 2000 as the initial value of the variable  GadTool .
2. Test Condition-: We added a condition that the loop can only continue if the value of variable  GadTool  is less than or equal to 2021.
3. Body of the loop-: We added an   if   statement to execute when the value of the division of variable  GadTool  by 4 is giving the remainder of zero (0), meaning that if the value of variable  GadTool  is a leap year, then print the current value of variable  GadTool .
4. Update Statement-: After the body of the   for   loop is executed then do  GadTool++ , this means that the loop increases the value of variable  GadTool  with 1.

The   for   loop continues until the value of  GadTool  is less than or equal to the limit of 2021.

Post a Comment

2 Comments

  1. Thank you so much. But I want you to also make an article to how I can get a list of all Leap Years between 2000 to 2021 using Java and JavaScript !!!
    I am waiting for the same article

    ReplyDelete

Translate