Python Program to count the number of days in a given month of a year

Number of days in a given month of a given year

 

Number of days in any month of a year can vary specifically in February as the cycle of leap year repeats in every 4 years when the year is leap February gives the count to 29 days but the when the year is not leap it gives count to 28 days and so no of days in a year varies from 365 to 366. 
Rather than February every month gives the count of 30 or 31 days in any case whether the year is a leap or not. 

Python program to count number of days in a given month of a year

Algorithm

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

Python Program for number of days

#take user inputs for Month and year in integer
Month = int(input('Enter the Month :'))
Year = int(input('Enter the Year :'))
#Check condition for Month and leap year
if(Month == 2 and (Year%4 == 0) or ((Year%100 == 0) and (Year%400 == 0))):
    #if condition is TRUE
    print('Number of days is 29')
#if False check for other conditions
elif(Month == 2):
    print('Number of days is 28')
elif(Month == 1 or Month == 3 or Month == 5 or Month == 7 or Month == 8 or Month == 10 or Month == 12):
    print('Number of days is 31')
else:
    print('Number of days is 30')
Output:
Enter the Month :2
Enter the Year :2012
Number of days is 29

5 comments on “Python Program to count the number of days in a given month of a year”


  • Om Prakash

    Java Code :
    import java.util.Scanner;
    public class Number_of_Days
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    System.out.print(“Enter month: “);
    int month = sc.nextInt();
    System.out.print(“Enter year: “);
    int year = sc.nextInt();
    sc.close();
    if(month==2 && ( (year%4==0 && year%100!=0) || year%400==0) )
    System.out.println(“Number of days is 29”);
    else if(month == 2)
    System.out.println(“Number of days is 28”);
    else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
    month == 10 || month == 12)
    System.out.println(“Number of days is 31”);
    else
    System.out.println(“Number of days is 30”);
    }
    }


  • Shubham Kumar

    #PROGRAM IN JAVA
    import java.util.*;
    public class NOofdayINYearMOnth {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println(“Enter any year: “);
    int y=sc.nextInt();
    System.out.println(“Enter month: “);
    int m=sc.nextInt();
    if(m==2)
    {
    if((y%400==0)||((y%4==0)&&(y%100!=0)))
    {
    System.out.println(“Number of days is 29”);
    }
    else
    {
    System.out.println(“Number of days is 28”);
    }
    }
    else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)System.out.println(“31”);
    else System.out.println(“Number of days is 30”);

    }

    }


      • Shubham Kumar

        #PROGRAM IN JAVA
        import java.util.*;
        public class NOofdayINYearMOnth {
        public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println(“Enter any year: “);
        int y=sc.nextInt();
        System.out.println(“Enter month: “);
        int m=sc.nextInt();
        if(m==2)
        {
        if((y%400==0)||((y%4==0)&&(y%100!=0)))
        {
        System.out.println(“Number of days is 29”);
        }
        else
        {
        System.out.println(“Number of days is 28”);
        }
        }
        else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)System.out.println(“Number of days is 31”);
        else System.out.println(“Number of days is 30”); } }