ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C program to find number of days in a month using switch case

This program will read month value and print total number of days in input month in C language.

In this c program, we will learn how to get number of days in a month. To get number of days in a month we are using switch case statement in this program.

Here, we are not using break after each switch case statement because we can check multiple case values for same body.

Note: Here we are not checking leap year, so we fixed 28 days in February.

ADVERTISEMENT

Program to find number of days in a month using C

#include <stdio.h>

int main()
{
	int month;
	int days;
	
	printf("Enter month: ");
	scanf("%d",&month);
	
	switch(month)
	{
		case  4:
		case  6:
		case  9:
		case 11:
			days=30;
			break;
		case  1:
		case  3:
		case  5:
		case  7:
		case  8:
		case 10:
		case 12:
			days=31;
			break;
			
		case 2:
			days=28;
			break;
		
		default:
			days=0;
			break;		
	}
	
	if(days)
		printf("Number of days in %d month is: %d\n",month,days);
	else
		printf("You have entered an invalid month!!!\n");
	
	return 0;
	
}

Output

First run:
Enter month: 3
Number of days in 3 month is: 31

Second run:
Enter month: 2
Number of days in 2 month is: 28

Third run:
Enter month: 11 
Number of days in 11 month is: 30 

Fourth run:
Enter month: 13 
You have entered an invalid month!!!

C Switch Case Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.