C Program for calculating the Number of days in a given month of a given year.

Telling the Number of days in a given month of a given year.

In this page we’ll take a look at a program using which we can determine the number of days in a given month of a given year. We’ll use simple If-Else Statements for our desired results with a corresponding print Statement in each block. Also, we’ll have to check for a the entered year to be a leap year since in that case February will have 29 days. 

C Program to find No. of days in a month in given year

Algorithm

 

  • Take the input from the user in the form of the month and the year of which he wants to know the number of days.
  • Use Conditional Statements (If-Else) to determine the number of days.
  • Since a leap year is completely divisible by 4, So, For a leap year use a conditional statement in which we check for the month February as well as a leap year.
  • Write print statements in the respective conditional blocks.

C Program

#include <stdio.h>
int main()
{ 
    int m;
    int y;
    printf("Please enter a number corresponding to a month.\n1 - January\n2 - February\n3 - March\n4 - April\n5 - May\n6 - June\n7 - July\n8 - August\n9 - September\n10 - October\n11 - November\n12 - December\n");
    scanf("%d",&m);
    printf("Please enter the year\n");
    scanf("%d",&y);
    if(m==1 ||m==3 ||m==5 ||m==7 ||m==8 ||m==10 ||m==12)
        printf("No. of days in this month is 31");//for months with 31 days
    else if(m==2 && y%4==0)                  
        printf("No. of days in this month is 29");//for a february in a leap year
    else if(m==2 && y%4!=0)                  
        printf("No. of days in this month is 28");//for a february in a normal year
    else        
        printf("No. of days in this month is 30");//for a month with 30 days
    return 0;
}
 
Please enter a number corresponding to a month.                                                                                                                               
1 - January                                                                                                                                                                   
2 - February                                                                                                                                                                  
3 - March                                                                                                                                                                     
4 - April                                                                                                                                                                     
5 - May                                                                                                                                                                       
6 - June                                                                                                                                                                      
7 - July                                                                                                                                                                      
8 - August                                                                                                                                                                    
9 - September                                                                                                                                                                 
10 - October                                                                                                                                                                  
11 - November                                                                                                                                                                 
12 - December                                                                                                                                                                 
2                                                                                                                                                                             
Please enter the year                                                                                                                                                         
2020                                                                                                                                                                          
No. of days in this month is 29