Counting number of days in a given month of a year

Method 1: 

Code

import java.io.*;

import java.util.*;

class PrepInsta

 

{

public static void main(String args[])

 {

 int month, year;

 Scanner sc = new Scanner(System.in);

System.out.println(“enter the month and year: “);

month=sc.nextInt();

year=sc.nextInt();

if(((month==2) && (year%4==0)) || ((year%100==0)&&(year%400==0)))

{

printf(“Number of days is 29”);

}

else if(month==2)

{

printf(“Number of days is 28”);

}

else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)

{

printf(“Number of days is 31”);

 }

else

{

printf(“Number of days is 30”);

}

}

}

Algorithm

  •  Start.
  •  Take user inputs like month and year.
  •  Check if the given month is February. 
  •  If True Check if the year is a year leap or not.
  •  If year is a leap year Print 29 Days, Else Print 28 Days.
  •  If Condition in Step 3 is False Check the month. 
  •  Print the number of days assigned to specific Month.
  •  End.

Output

Output:
enter the month and year
1
2021
Number of days is 31
Get Access To Our Premium Courses
Install our application from PlayStore and get discounts on our new courses.

Pin It on Pinterest