Leap Year or Not

  Java program to find given year is leap year or not by using if, else-if, nested-if statement and method.

There are different ways to find given year is leap year or not in Java.

  1. Write a Java Program to find given number is Leap or Not using if
  2. Write a Java Program to find given number is Leap or Not using else-if.
  3. Write a Java Program to find given number is Leap or Not using Nested if statement
  4. Write a Java Program to find given number is Leap or Not using method

Program 1:Write a Java Program to find given number is Leap or Not using if statement

import java.util.Scanner;
public class Leap 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Year: ");
		int Year = s.nextInt();
			if((Year%4==0&&Year%100!=0)||Year%400==0)
			{
			System.out.println("\n "+Year + " is Leap Year");
			}
			else
			{
			System.out.println("\n "+Year+" is a NOT Leap Year. ");
			}
	}
}
Output:

Enter Year: 

2022

2022 is a NOT Leap Year. 


import java.util.Scanner;
public class Leap {
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Year: ");
		int Year = s.nextInt();
		if ( Year % 400 == 0) 
		{
			System.out.println("\n"+Year+" is a Leap Year. ");
		}
		else if (Year%100 == 0) 
		{
			System.out.println("\n"+Year+" is NOT a Leap Year. ");
		}
		else if(Year%4 == 0) 
		{
			System.out.println("\n"+Year+" is a Leap Year. ");
		}
		else 
		{
			System.out.println("\n"+Year+" is a NOT Leap Year. ");
		}	
	}
}

Output:

Enter Year:  2014

 2014 is a NOT Leap Year. 
import java.util.Scanner;
public class Leap 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("Enter Year: ");
		int year = s.nextInt();
		if ( year % 4 == 0) 
		{
		        if (year%100 == 0) 
			{
			   if(year%400 == 0) 
			   {	
			   System.out.println("\n"+year+" is a Leap Year. ");
			   }
			   else 
			   {
			   System.out.println("\n"+year+" is a NOT Leap Year.");
			   }
			}
			else 
			{
			    System.out.println("\n"+year+" is a Leap Year. ");
			}
		}
		else 
		{
		   System.out.println("\n"+year+" is a NOT Leap Year. ");
		}
	  }
}
Output:
Enter Year: 
2010

2010 is a NOT Leap Year. 
import java.util.Scanner;
public class Leap 
{
	void leapYearOrNot()
	{
		Scanner s = new Scanner(System.in);
		System.out.println("Enter Year: ");
		int Year = s.nextInt();
			if((Year%4==0 && Year%100!=0)||Year%400==0)
			{
				System.out.println(Year + " is Leap Year");
			}
			else
			{
				System.out.println("It is Not a Leap Year");
			}
	}
	public static void main(String[] args) 
	{
		Leap obj = new Leap();
		obj.leapYearOrNot();
	}
}
  
Output:
Enter Year: 
2010

2010 is a NOT Leap Year. 

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32