- Write a C program to print number of days in a month using if else ladder statement.
Required Knowledge
- C printf and scanf functions
- If Else Ladder in C
In this program, we will take a number between 1 to 12 as input from user, where 1 corresponds to January, 2 corresponds to February and so on. We will use if else ladder statement to print number of days in any month in words.
C program to print number of days in months
/* * C program to print Number of Days in any Month */ #include <stdio.h> int main() { int month; /* * Take the Month number as input form user */ printf("Enter Month Number (1 = January ..... 12 = December)\n"); scanf("%d", &month); /* Input Validation */ if(month < 1 || month > 12){ printf("Invalid Input !!!!\n"); return 0; } if(month == 2) { printf("28 or 29 Days in Month\n"); } else if(month == 4 || month == 6 || month == 9 || month == 11) { printf("30 Days in Month\n"); } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { printf("31 Days in Month\n"); } return 0; }
Output
Enter Month Number (1 = January ..... 12 = December) 2 28 or 29 Days in Month
Enter Month Number (1 = January ..... 12 = December) 12 31 Days in Month
Enter Month Number (1 = January ..... 12 = December) 9 30 Days in Month
Enter Month Number (1 = January ..... 12 = December) 15 Invalid Input !!!!