Question: Write a program that takes a year as the input from stdin and uses the modulus operator to determine if the year is a leap

Write a program that takes a year as the input from stdin and uses the modulus operator to determine if the year is a leap year or not. Print the result to the screen.
Here is the algorithm to check if a year is a leap year or not:
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
Here is what I have so far:
#include
int main(void)
{
int year;
char c;
printf("Enter a year: ");
//use scanf to accept user input
//check that expected data type was included in the user input
//check that nothing else after the valid user input was included
//use while loop to iterate forever until valid inout is received
while((scanf("%d", &year)!=1)||((c = getchar())!='
' && c != EOF))
{
printf("ERROR: Invalid input. Please enter new value: ");
while ((c = getchar())!='
' && c != EOF); // flush input buffer before next scanf()
}
// TO DO: determine if input year is a leap year
if(year %4!=0)
{
printf("%d is not a leap year.
");
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!