Write a Program to find the number of days in a month, considering leap year, using Switch statement.
The controls statement that allows us to make a decision from a number of choices is called a switch statement, or more correctly a switch-case-default, since these three keywords go together to make up the control statement. The syntax for switch statement is as follows:
GET INSTANT HELP FROM EXPERTS!
- Looking for any kind of help on your academic work (essay, assignment, project)?
- Want us to review, proofread or tidy up your work?
- Want a helping hand so that you can focus on the more important tasks?
Hire us as project guide/assistant. Contact us for more information
switch (expression)
{
case constant 1: statement;
break;
case constant 2: statement;
break;
‘
‘
case constant n; statement;
break;
default: statement;
}
The integer expression following the keyword switch is any repression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer. The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. The statement lines in the above form of switch represent any valid C statement.
When we run a program containing a switch, first, the integer expression following the control to the statement following the keyword switch is evaluated. the value it gives is then matched, one-by-one, against the constant values that follow the case statements. When a match is found, the program executes the statements following that case up to the break statement. Break transfers control to the statement following the switch case.
The switch statement is very useful in writing menu driven program.
A year is a leap year if on dividing by 4 it leaves remainder as 0 but not by 100, or is divisible by 400. Thus, 1996, 2000 and 2004 are leap years but 1900, 2002 and 2100 are not.
Algorithm:
1. Start
2. Declare the variable for month number.
3. Accept month number from user.
4. By using Switch statement, select the no. of days according to month number
4.1 If month number (1||3||5 ||7||8||10||12), then Output=31. Display output.
4.2 If month number (4||5||6||9||11), then Output=30. Display output.
4.3 If month number (2), then
4.3.1 Accept the year number
4.3.2 Check whether the entered year is leap year or not
4.3.3 If leap year, then output=29
4.3.4 Else, output=28
Case control statement, i.e. the Switch statement is used to design many driven program. So using switch statement a menu driven program to find the number of days of a month was implemented here.
StudyMumbai.com is an educational resource for students, parents, and teachers, with special focus on Mumbai. Our staff includes educators with several years of experience. Our mission is to simplify learning and to provide free education. Read more about us.
Leave a Reply
You must be logged in to post a comment.