C++ program to get number of days in month,year using nested if

Introduction: In previous articles i explained the program to calculate sum,average and division of marks in C++ Language and  Else if ladder or multiple else if to check the day corresponding to number in C++ Language and Else if ladder to perform operation on two numbers based on operator and How to find greatest of three numbers using multiple if statement and How to find month name corresponding to number using switch case.

In this article i am going to write a program to get the number of days in a month of particular year. On executing the it will prompt you to enter Year and  month. And Based on the entered month and year 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 and depending on that calculates the number of days in that year. So basically this program  covers the following:
  • Checking whether year is a leap year or not
  • Display Month name corresponding to entered number e.g. display July if 7 is entered as month
  • Calculates number of days in the month
  • Calculates 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<iostream.h>
void main()
{
 int year,month;
 clrscr();
 cout<<"Enter year:=";
 cin>>year;
 cout<<"Enter month:=";
 cin>>month;
 cout<<"\n\n==========================================\n\n";
  if(year<=9999 && year>=1100)
   {
    if((year%400==0)||((year%100!=0)&&(year%4==0)))
     {
      cout<<year<<" :: is a leap year with 366 days\n";
      if(month==1)
       {
                cout<<"January\nDays: 31";
       }
      else if(month==2)
       {
                cout<<"February\nDays: 29";
       }
      else if(month==3)
       {
                cout<<"March\nDays: 31";
       }
      else if(month==4)
       {
                cout<<"April\nDays: 30";
       }
      else if(month==5)
       {
                cout<<"May\nDays: 31";
       }
      else if(month==6)
       {
                cout<<"June\nDays: 30";
       }
      else if(month==7)
       {
                cout<<"July\nDays: 31";
       }
      else if(month==8)
       {
                cout<<"August\nDays: 31";
       }
      else if(month==9)
       {
                cout<<"September\nDays: 30";
       }
      else if(month==10)
       {
                cout<<"October\nDays: 31";
       }
      else if(month==11)
       {
                cout<<"November\nDays: 30";
       }
      else if(month==12)
       {
                cout<<"December\nDays: 31";
       }
      else
       {
                cout<<"Invalid month...please enter form 1 to 12";
       }
      }
    else
     {
      cout<<year<<":: with 365 days\n";
      if(month==1)
       {
                cout<<"January\nDays: 31";
       }
      else if(month==2)
       {
                cout<<"February\nDays: 28";
       }
      else if(month==3)
       {
                cout<<"March\nDays: 31";
       }
      else if(month==4)
       {
                cout<<"April\nDays: 30";
       }
      else if(month==5)
       {
                cout<<"May\nDays: 31";
       }
      else if(month==6)
       {
                cout<<"June\nDays: 30";
       }
      else if(month==7)
       {
                cout<<"July\nDays: 31";
       }
      else if(month==8)
       {
                cout<<"August\nDays: 31";
       }
      else if(month==9)
       {
                cout<<"September\nDays: 30";
       }
      else if(month==10)
       {
                cout<<"October\nDays: 31";
       }
      else if(month==11)
       {
                cout<<"November\nDays: 30";
       }
      else if(month==12)
       {
                cout<<"December\nDays: 31";
       }
      else
       {
                cout<<"Invalid month...please enter form 1 to 12";
       }
      }
     }
    else
    {
     cout<<"Invalid year..Please enter year between 1100 to 9999";
    }
   getch();
 }

Output:
First Run
Enter year := 2011
Enter month := 2
==========================
2011 :: 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:= 4
==========================
2013 :: with 365 days
April
Days: 30

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