Question

Part1. Write a program that reads an integer value from the user representing a year. The...

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..

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A year with 366 days is called a leap year. A year is a leap year...
A year with 366 days is called a leap year. A year is a leap year if it is divisible by four (for example, 1980), except that it is not a leap year if it is divisible by 100 (for example, 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). There were no exceptions before the introduction of the Gregorian calendar on October 15, 1582 (1500 was a leap year). Write a program...
Need this program in Java Develop (using C++ or Java language) the software application described below....
Need this program in Java Develop (using C++ or Java language) the software application described below. Write a program that prints the day number of the year, given the date is in the form month-day-year. For example, if the input is 1-1-09, the day number is 1; if the input is 12-25-09, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4 but not divisible...
Script #2 – Leap Year Description:  Given a year, report if it is a leap year. Purpose:...
Script #2 – Leap Year Description:  Given a year, report if it is a leap year. Purpose: Learn the use of if statements. Instructions:  Complete this assignment to make sure that you know how to use if statements in Powershell. A leap year in the Gregorian calendar occurs: on every year that is evenly divisible by 4 except every year that is evenly divisible by 100 unless the year is also evenly divisible by 400 Write a script that prompts a user...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
Write a program in C++ that reads integer values from standard input and writes to standard...
Write a program in C++ that reads integer values from standard input and writes to standard output the smallest of the inputs and the average input value. The program should stop reading when a value equal to -1 or greater than 100 is encountered. It should also stop when an incorrect integer value (i.e., anything that isn't an int) is entered. If there is not an integer value in the input, the program should output "no integers provided"
Write a Python program that reads in the month, day, and year of a date and...
Write a Python program that reads in the month, day, and year of a date and prints it in the dd/mm/yyyy format or mm/dd/yyyy format, the format depending on the user’s preference. The program also prints the message It is a magic date If the product of month and day is equal to the last two digits of the year. For example, April 20 1980 is a magic date because April (numeric value 4) multiplied by 20 is equal to...
Write a program which inputs an integer value, incr, from the user at the keyboard, and...
Write a program which inputs an integer value, incr, from the user at the keyboard, and then displays a graph of the sine of the degree values from 0 to 180, in increments of incr. You must use the sin function (in a similar manner to ceil and floor) to find the sine. Note that the sin function only operates on radians so the degree value must first be converted to radians. To do so, multiply the degree value by...
Instructions PowerShell Assignment #1 Objective: Learn basic Powershell scripting in Windows Setup: A Windows computer with...
Instructions PowerShell Assignment #1 Objective: Learn basic Powershell scripting in Windows Setup: A Windows computer with the latest Powershell environment installed Script #1 – Hello World! Description:  The classical introductory exercise: Just say "Hello, World!". Purpose: A "Hello, World!" program is traditionally used to introduce novice programmers to a programming language. It is also to make sure that the interpreter is installed correctly, and that the user understands how to use it. Instructions:  Complete this assignment to make sure that you know...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
C++. Write a program that draws a rocket shape on the screen based on user input...
C++. Write a program that draws a rocket shape on the screen based on user input of three values, height, width and stages. The type of box generated (i.e.. a hollow or filled-in) is based on a check for odd or even values input by the user for the box height (or number of rows). Here is the general specification given user input for the height of the box..... Draw a hollow box for each stage if the value for...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT