Counting number of days in a given month of a year

Java Program to find number of days in a given month of a year

Number of days in a month can vary depending on a year that is leap year or not .Suppose a year is leap year then February month will consist of 29 days .Suppose a year is not leap year then February month will consist of 28 days .Months like January, march, may , July , august ,October ,December consist of 31 days Irrespective of leap year or not. Months like April ,June ,September , November consist of 30 days irrespective of leap year or not .

java program for counting numbers of days in a given month of a year

Implementation:

  •  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.

Code in Java:

import java.io.*;
import java.util.*;
class PrepInsta
{
    public static void main(String args[])
    {
    int monthyear;
    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”);
    }
}
}
Output:

enter the month and year

1

2021

Number of days is 31