C program to find number of days in a given month and year; Through this tutorial, we will learn how to find number of days in given month and year in c program.
Algorithm to Find Number of Days in a Given Month and Year
Use the following algorithm to write a program to find number of days in a given month and year; as follows:
- Step 1:- Start Program.
- Step 2:- Get input month and year from user.
- 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 Program.
C Program to Find Number of Days in a Given Month and Year
#include<stdio.h>
int main()
{
int month, year;
printf("enter the month : ");
scanf("%d",&month);
printf("enter the year : ");
scanf("%d",&year);
if(((month==2) && (year%400==0)) || ((year%100!=0)&&(year%4==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");
}
return 0;
}
The output of the above c program; as follows:
enter the month : 2 enter the year : 2022 Number of days is 28