0 0
Read Time:51 Second

Write a Java program to count the number of days between two given years.

import java.util.Scanner;
class NoDays_TwoYears
{
	public static void main(String[] args)
	{
		System.out.print("Enter The Start Year :");
		Scanner s = new Scanner(System.in);
		int fy = s.nextInt();
		System.out.print("Enter The End Year :");
		s = new Scanner(System.in);
		int ey = s.nextInt();
		if (ey > fy)
		{
			System.out.println("\nYear & Number of Days " );
			for (int i = fy; i <= ey; i++)
			{
				System.out.println( i + " = " + num_of_Days(i));
			}
		}
		else
		{
			System.out.println("End Year must be Greater than First Year ..");
		}
	}
	public static int num_of_Days(int year)
	{
		if (leap_Year(year))
			return 366;
		else
			return 365;
	}
	public static boolean leap_Year(int year)
	{
		return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
	}
}
Output:
Enter The Start Year :2014
Enter The End Year :2022

Year & Number of Days
2014 = 365
2015 = 365
2016 = 366
2017 = 365
2018 = 365
2019 = 365
2020 = 366
2021 = 365
2022 = 365
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment

Verified by MonsterInsights