Part1. Write a program that reads an integer value from the user
representing a year. The purpose of the program is to determine if
the year is a leap year (has 29 days in February) in the Gregorian
calendar. A year is a leap year if it is divisible by 4, unless it
is also divisible by 100 but not 400. For example, the year 2003 is
not a leap year, but 2004 is. The year 1900 is not a leap year
because it is divisible by 100 but not 400. The year 2000 is a leap
year because it is divisible by 100 and is also divisible by 400.
Produce an error message if the input value is less than 1582 (the
year the Gregorian calendar was adopted). Run the test part1 before
starting.
run part one before part 2.
Part2. Modify the code of part1 so that user can evaluate multiple years. Allow the user to terminate the program using a sentinel value. Validate each input value to make sure it is greater than or equal to 1582..
Part1:
#include <stdio.h>
int main(void) {
int year;
printf("Enter the YEAR(YYYY): ");
scanf("%d",&year);
if(year>1582)
{
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
}
else
{
printf("Invalid year");
}
return 0;
}
Output:
Enter the YEAR(YYYY): 2004
2004 is a leap year.
PART 2;
#include <stdio.h>
int main(void) {
int year;
int flag=1;
while(flag==1) //check sentinel value
{
printf("Enter the YEAR(YYYY): "); // Take input
scanf("%d",&year);
if(year>=1582) // check if the year is greater than 1582
{
if(year%4 == 0) // implementing the leap year algorithm
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
}
else
{
printf("Invalid year");
}
// Asking user to continue or not
printf("Do you want to continue?\nEnter 1 for yes\nEnter 0 for
No");
scanf("%d",&flag);
}
printf("Program Terminated.");
return 0;
}
Output:
Enter the YEAR(YYYY): 2004
2004 is a leap year.
Do you want to continue?
Enter 1 for yes
Enter 0 for No
1
Enter the YEAR(YYYY): 2018
2018 is not a leap year.
Do you want to continue?
Enter 1 for yes
Enter 0 for No
1
Enter the YEAR(YYYY): 2005
2005 is not a leap year.
Do you want to continue?
Enter 1 for yes
Enter 0 for No
0
Program Terminated.
Get Answers For Free
Most questions answered within 1 hours.