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 in  Python

Here, in this page we will discuss the program to find the number of days in a given month of a given year in python . 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.

Number of days in a given month of a year in python

Method Discussed :

  • Method 1: Using if-else ladder.
  • Method 2 : Using Array

Method 1 :

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

Method 1 : Code in Python

Run
month = 12
year=2012
    
if((month==2) and ((year%4==0)  or ((year%100==0) and (year%400==0)))) :
    print("Number of days is 29");

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

Number of days is 31

Method 2 :

In this method instead of using if-else-if ladder or switch case, we use array.

Method 2 : Code in Python

Run
arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
month = 12
year=2012
    
if(month==2 and ((year%400==0) or ((year%100!=0) and (year%4==0)))) :
    print("Number of days is ", arr[month-1]+1)
    
else :
    print("Number of days is ", arr[month-1]);

Output

Number of days is 31

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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


  • GSaiCharan

    arr=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
    m=int(input(“enter a month:”))
    y=m-1
    b=int(input(“enter a year:”))
    if y==1 and (b%400==0) or (b%4==0 and b%100!=0 ):
    arr[1]=29
    print(f”the number dys in a month are{arr[y]}”)


  • cherith

    list1=[1,2,3,4,5,6,7,8,9,10,11,12]
    list2=[31,28,31,30,31,30,31,31,30,31,30,31]

    for i in range(0,12):
    if month == 2:
    if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print(“29 days “)
    break
    else:
    if list1[i]==month:
    print(list2[i],”days”)


  • 121910307021

    def monyear(m,y):
    s=str(y)
    if(len(s)==4 and m<13):
    if(m==2) and (y%4 == 0) or (y%400==0):
    noofdays = 29
    elif(m==2):
    noofdays =28
    elif(m==1 or m==3 or m==5 or m==7 or m==8 or m ==10 or m==12 ):
    noofdays =31
    else:
    noofdays =30
    return print("no of days are ",noofdays)
    else:
    print("enter valid year")

    monyear(int(input("enter month ")), int(input("enter year ")))


  • Deepak

    #include
    using namespace std;
    int main()
    {
    int Month,Year;
    cin>>Month;
    cin>>Year;
    if(Month == 2 && (Year%4 == 0) || ((Year%100 == 0) && (Year%400 == 0)))

    cout<<"Number of days is 29";
    //if False check for other conditions
    else if(Month == 2)
    cout<<"Number of days is 28";
    else if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
    cout<<"Number of days is 31";
    else
    cout<<"Number of days is 30";
    return 0;
    }


  • Pradeep

    //check no. of days in month in C++

    #include
    using namespace std;
    int check_year(int);
    int main()
    {
    int year,month;
    cout<>year;
    cout<>month;
    if(month==2)
    {
    int result=check_year(year);
    if(result==1)
    cout<<"29 days";
    else
    cout<<"28 days";
    }
    else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
    cout<<"31 days";
    else if(month==4 || month==6 || month==9 || month==11)
    cout<<"30 days";
    else
    cout<<"wrong input";
    return 0;
    }
    int check_year(int year)
    {
    if(year%4==0 && (year%100!=0 || year%400==0))
    return 1;
    else
    return 0;
    }


  • 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”);
    }
    }


  • Sneha Rani

    #JAVA_CODE
    —————–
    package programs;

    import java.util.Scanner;

    public class NumberOfDaysInaMonth {

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print(“Enter a Month: “);
    int mnth=sc.nextInt();
    System.out.print(“Enter a Year: “);
    int yr=sc.nextInt();
    if(mnth == 2 || (yr % 4 == 0) && (yr % 100 == 0 )|| (yr % 400 == 0))
    System.out.println(“No of days are 29”);
    else if (mnth == 2)
    System.out.println(“No of days are 28”);
    else if(mnth ==1 || mnth ==3 || mnth ==5 || mnth ==7 || mnth ==8 || mnth ==10 || mnth ==12)
    System.out.println(“No of days are 31”);
    else
    System.out.println(“No of days are 30”);
    sc.close();

    }

    }


  • placement

    program in c language given below:

    #include
    void main()
    {
    int year,month;
    printf(“enter the year:\t”);
    scanf(“%d”,&year);
    printf(“enter the month:”);
    scanf(“%d”,&month);

    if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
    {
    printf(“31 days”);
    }
    else if(month == 2)
    {
    if(year%4==0)
    {
    if(year%100==0)
    {
    if(year%400==0)
    {
    printf(“29 days”);
    }
    else{
    printf(“28 days”);
    }
    }
    else
    {
    printf(“29 days”);
    }
    }
    else
    {
    printf(“28 days”);
    }
    }
    else
    {
    printf(“30 days”);
    }
    }


  • 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”); } }


        • Anmol

          Kinda silly ! Everyone keeps writing all the month number apart from using logic
          here it should help from python
          m=int(input(“Enter month”))
          y=int(input(“Enter year:”))
          if m==2 and (y%4==0) and (y%100!=0) or (y%400==0):
          print(“29 Days”)
          elif m==2:
          print(“28 Days”)
          elif m<=12 and m%2!=0:
          print("31 Days")
          else:
          print("30 Days")

          0