In this tutorial you will learn about the C Program to Find Number of Days in a Month and its application with practical example.
C Program to Find Number of Days in a Month
In this tutorial, we will learn to create a C program that will Find the Number of Days in a Month using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Conditional Statements in C programming.
- Arithmetic operations in C Programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the required variables for the program. 2. Taking the input from the user. 3. Using the conditional statements to find days. 4. Printing the Days in a month. 5. End the Program. |
Find Number of Days in a Month:-
In today’s program, we will take the input month in a range between 1 to 12 from the user. Then we will pass that variable to the conditional statements to find the number of days. At last, we will print the resulting number of days to the user.
With the help of the below program, we can find the number of days in a month.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/** * C program to print number of days in a month */ #include <stdio.h> int main() { //Declaring the required vaiable for the program. int month; /* Input month number from user */ printf("Enter the month number from (1-12): "); /* Scanning the input from the user */ scanf("%d", &month); //Finding the number of days in a month. if(month == 1) { //Printing the number of days in a month printf("31 days"); } else if(month == 2) { //Printing the number of days in a month printf("28 or 29 days"); } else if(month == 3) { //Printing the number of days in a month printf("31 days"); } else if(month == 4) { //Printing the number of days in a month printf("30 days"); } else if(month == 5) { //Printing the number of days in a month printf("31 days"); } else if(month == 6) { //Printing the number of days in a month printf("30 days"); } else if(month == 7) { //Printing the number of days in a month printf("31 days"); } else if(month == 8) { //Printing the number of days in a month printf("31 days"); } else if(month == 9) { //Printing the number of days in a month printf("30 days"); } else if(month == 10) { //Printing the number of days in a month printf("31 days"); } else if(month == 11) { //Printing the number of days in a month printf("30 days"); } else if(month == 12) { //Printing the number of days in a month printf("31 days"); } else { //Printing the number of days in a month printf("Invalid input! Please enter month number between the given range of (1-12)."); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- month = it will hold the integer value for the input.
Taking the input month from the user.
Finding the number of days in a month.
Printing output.