Question

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 in C++ that asks the user for a year and computes whether that year is a leap year.

Homework Answers

Answer #1

Please find the code for the following:

Code:

#include <iostream>
using namespace std;

int main()
{
int year;
//Accept the user input
cout<<"Enter a year: ";
cin>>year;
  
//We need to change the order of the conditions which is mentioned in the question
//Because - Consider the year 1900, in the first condition which is provided in the
//question, we need to check if it is divisible by 4 then 100.
//1900 is divisible by 4, as per the Condition it should be a leap year
//But 1900 is not a leap year, since as per the second Condition it is divisible by 100
//So, To avoid all this, I just changeed the order as per the logic
  
//Since there are no Exceptions for the year till 1582, we made extra cases for that
if (year < 1582 && year % 4 == 0)
cout<<year<<" is a leap year";
else if (year < 1582)
cout<<year<<" is not a leap year";
else if (year >= 1582)
{
//Condition 3: It is a leap year if it is divisible by 400
//check if it is divisible by 4, then it is a leap year
if(year%400==0)
cout<<year<<" is a leap year";
  
//Condition 2: It is not a leap year if it is divisible by 100
//Check if it is divisible by 100,then it is not leap year
else if(year%100 == 0)
cout<<year<<" is a not a leap year";
  
//Condition 1: A year is a leap year if it is divisible by four
//Check if it is divisible by 4, then it is a leap year
else if(year % 4==0)
cout<<year<<" is a leap year";
  
//Finally, if is not divisible by any ,then not a leap year
else
cout<<year<<" is a not leap year";
}
return 0;
}

Please check the compiled program and its output for your reference:

Output:
Sample case-1:


Sample case-2:

Sample case-3:

Sample case-4:

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

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
Leap Year The month of February normally has 28 days. But if it is a leap...
Leap Year The month of February normally has 28 days. But if it is a leap year, February has 29 days. Write a program that asks the user to enter a year. The program should then display the number of days in February that year. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and if only it is also divisible by 400....
3.21 LAB C++ Code: Leap Year A year in the modern Gregorian Calendar consists of 365...
3.21 LAB C++ Code: Leap Year A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2) If...
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...
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...
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...
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...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...
Discuss ethical issues that can be identified in this case and the mode of managing ethics...
Discuss ethical issues that can be identified in this case and the mode of managing ethics Enron finds itself in this case. How would you describe the ethical culture and levels of trust at Enron? Provide reasons for your assessment. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT