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...
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...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described itself as "a quarterly survey of business leaders from across the globe … surveying 11,500 businesses in 40 economies across the globe on an annual basis." 1 According to the 2011 IBR, the Asia Pacific region had a higher percentage (27 percent) of female chief executive officers (CEOs) than Europe and North America. Japan is the only Asia Pacific region exception. The report further...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect on what happens to the families when there is a suicide in the family, based on the Seasons of Love chapter...how should people be told? What details are best left unshared? below is the story These theories may have a certain face-validity, but they often neglect environmental or contextual factors that are innate to answering the question of “why” a person might engage in...
In February 2012, the Pepsi Next product was launched into the US market. This case study...
In February 2012, the Pepsi Next product was launched into the US market. This case study provides students with an interesting insight into PepsiCo’s new product process and some of the challenging decisions that they faced along the way. Pepsi Next Case Study Introduction Pepsi Next was launched by PepsiCo into the US market in February 2012, and has since been rolled out to various international markets (for instance, it was launched in Australia in September 2012). The new product...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT