Please disable your adblock and script blockers to view this page
 

C program to get number of days in month and year using else if ladder

Introduction: In previous articles i explained the Program to calculate sum,average and division of marks using else-if ladder  and  Program to calculate sum,average of marks in C Language and Else if ladder or multiple else if to check the day corresponding to number and Else if ladder or multiple else if to check the month of entered number.

In this article i have written a program to get the number of days in a month of particular year. When you execute the program it will prompt you to enter Year and  month. Based on the month and year entered it will calculate the number of days in that month and total days in the year. It will also handle the number of days variation due to leap year by first checking whether the year is leap year or not.So basically this program  covers the following:
  • Checking whether year is a leap year or not
  • Get Month name corresponding to entered number
  • Get number of days in the month
  • Get numbers of days in the year
Let's move forward and create the program.

Note: I have limited the year to be entered between 1100 to 9999. It means you can enter year between 1100 to 9999. But you can also change it as per your requirement.

#include<conio.h>
#include<stdio.h>
void main()
{
int year,month;
clrscr();
printf("Enter year:=");
scanf("%d",&year);
printf("Enter month:=");
scanf("%d",&month);
printf("\n\n==========================================\n\n");
if(year<=9999 && year>=1100)
{
 if((year%400==0)||((year%100!=0)&&(year%4==0)))
  {
   printf("%d :: is a leap year with 366 days\n",year);
   if(month==1)
    {
     printf("January\nDays: 31");
    }
   else if(month==2)
    {
     printf("February\nDays: 29");
    }
   else if(month==3)
    {
     printf("March\nDays: 31");
    }
   else if(month==4)
    {
     printf("April\nDays: 30");
    }
   else if(month==5)
    {
     printf("May\nDays: 31");
    }
   else if(month==6)
    {
     printf("June\nDays: 30");
    }
   else if(month==7)
    {
     printf("July\nDays: 31");
    }
   else if(month==8)
    {
     printf("August\nDays: 31");
    }
   else if(month==9)
    {
     printf("September\nDays: 30");
    }
   else if(month==10)
    {
     printf("October\nDays: 31");
    }
   else if(month==11)
    {
     printf("November\nDays: 30");
    }
   else if(month==12)
    {
     printf("December\nDays: 31");
    }
   else
    {
     printf("Invalid month...please enter form 1 to 12");
    }
  }
  else
  {
   printf("%d :: with 365 days\n",year);
   if(month==1)
   {
    printf("January\nDays: 31");
   }
   else if(month==2)
   {
    printf("February\nDays: 28");
   }
   else if(month==3)
   {
    printf("March\nDays: 31");
   }
   else if(month==4)
   {
    printf("April\nDays: 30");
   }
   else if(month==5)
   {
    printf("May\nDays: 31");
   }
   else if(month==6)
   {
    printf("June\nDays: 30");
   }
   else if(month==7)
   {
    printf("July\nDays: 31");
   }
   else if(month==8)
   {
    printf("August\nDays: 31");
   }
   else if(month==9)
   {
    printf("September\nDays: 30");
   }
   else if(month==10)
   {
    printf("October\nDays: 31");
   }
   else if(month==11)
   {
    printf("November\nDays: 30");
   }
   else if(month==12)
   {
    printf("December\nDays: 31");
   }
  else
   {
    printf("Invalid month...please enter form 1 to 12");
   }
  }
 }
 else
 {
  printf("Invalid year...please enter year between 1100 to 9999");
 }
 getch();
}

Output:
First Run
Enter year := 2001
Enter month := 2
==========================
2001 :: with 365 days
February
Days: 28

Second Run:
Enter year:= 2012
Enter month:= 2
==========================
2012 :: is a leap year with 366 days
February
Days: 29

Third Run:
Enter year:= 2013
Enter month:= 7
==========================
2013 :: with 365 days
February
Days: 28

Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon