Write a program (C language) that will read the number of a month and will print the number of days in the month. Ignore leap years. Use 28 days for February.
Have the program runs in a continuous loop, allowing the user to enter a month number, see that number of days, and repeat. Use month number = 0 to exit the loop and the program.
Program must meet the following criteria:
1.Your name and the name of the program embedded in a comment, and displayed on execution.
2.Ask the user to enter a month number.
3.Use if or if-else statements to decide how many days are in the month entered. Set a “day” equal to the number of days in the month ( if entered 2 (February) for the month, then day=28).
4.Include if or if-else statement(s) (decision) to make sure the user has entered a valid value for the month, between 1 and 12. If entry is invalid, ex..13, then display a polite error message, and no number-of-days display.
5.Send a message to the user with the month number and the number of days in that month (ex.“Your month was 2 and it has 28 days.”).
6. Do not use 12 or 13 if statements. Use 5 MAXIMUM. You can use one if statement for all of the months that have 31 days; another for all of the months that have 30 days; and one for Feb.
#include<stdio.h> // header file for standard input and output
int main()
{
int day; //define the variable day as integer type
printf("Name \n Program to display number of days in a month");
//Replace the name with your name. To print the name and program name
while(1) //continuous loop
{
printf("\n Enter a month number");
scanf("%d",&day);
if(day==1||day==3||day==5||day==7||day==8||day==10||day==12) //Logical OR operator is used to check either cases
printf("Your month was %d and it has 31 days",day);
else if(day==4||day==6||day==9||day==11)
printf("Your month was %d and it has 30 days",day);
else if(day==2)
printf("Your month was %d and it has 28 days",day);
else if(day>12) //If entered number is greater than 12
printf("Error message: Please enter a valid number from 1 to 12");
else if(day==0)
break; // exit the loop
}
return 0;
}
//after entering the month number please press enter to receive the ouput
Get Answers For Free
Most questions answered within 1 hours.