Question

CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap...

CIS22A Homework 6

Topic: Chapter 4 & 5

Purpose: Determine if a year is a leap year

Create a new C++ project titled “HW 6” in the CodeBlocks IDE. Use the “Console Application” project option.

Over time, the calendar we use has changed a lot. Currently, we use a system called the Gregorian Calendar and it was introduced in 1582.

The algorithm to determine if a year is a leap year is as follows:

Every year that is exactly divisible by four is a leap year, except for years that are divisible by 100 (century years); but these centurial years are leap years, if they are divisible by 400.

For example, the century years 1700, 1800, and 1900 were not leap years, but the years 1600 and 2000 were because these are divisble by 400.

Leap year calculator

Requirements:

Program must use a sentinel loop (slide 5.8) using value -1 to stop and to run continuously with mutiple year inputs until user enter -1 to stop.

There must be 2 functions: main() and isLeap()

main() function should do the following

-Check that the input year is not less than 1582. Iff it is, display an error message and continue to loop

-If the year is valid, main() calls the isLeap() function and gets a boolean result (true or false). Then it displays a text based on the boolean result.

Write a function using this exact prototype before main() (Chapter 6):

bool isLeap(int year);

This function includes the decision statements to determine if the year is a leap year or not. It returns true or false.

There is no "cin", no "cout", and no loop in this function. It only has "if / else" statement and return.

The function definiton should be put after main().

Here are some sample program run

This program determines leap year.

Enter a year after 1582 or enter -1 to exit: 2000

The year 2000 is a leap year.

Enter a year after 1582 or enter -1 to exit: 1600

The year 1600 is a leap year.


Enter a year after 1582 or enter -1 to exit: 1700

The year 1700 is not a leap year.


Enter a year after 1582 or enter -1 to exit: 2003

The year 2003 is not a leap year.

Enter a year after 1582 or enter -1 to exit: 1900

The year 1900 is not a leap year.

Enter a year after 1582 or enter -1 to exit: 1904

The year 1904 is a leap year.

Enter a year after 1582 or enter -1 to exit: 2004

The year 2004 is a leap year.

Enter a year after 1582 or enter -1 to exit: 1004

The year 1004 is not a valid year

Enter a year after 1582 or enter -1 to exit: -1234

The year -1234 is not a valid year

Enter a year after 1582 or enter -1 to exit: 2400

The year 2400 is a leap year.

Enter a year after 1582 or enter -1 to exit: 2800

The year 2800 is a leap year.


0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

bool isLeap(int year);

int main() {
    cout << "This program determines leap year." << endl;
    int year;
    while (true) {
        cout << "Enter a year after 1582 or enter -1 to exit: ";
        cin >> year;
        if (year == -1)
            break;
        if (year < 1582) {
            cout << "invalid year" << endl;
        } else {
            if (isLeap(year)) {
                cout << "The year " << year << " is a leap year." << endl;
            } else {
                cout << "The year " << year << " is not a leap year." << endl;
            }
        }
    }
    return 0;
}

bool isLeap(int year) {
    return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
Add a comment
Know the answer?
Add Answer to:
CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design an application with a single class and two static methods: method main and method isLeap....

    Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not. public static boolean isLeap ( int year ) The year is leap year if it is divisible by 4, but not divisible by 100 , except if it is divisible by 400. Examples 2003 is not a leap year 2020 is leap year...

  • leap year C++

    the instructions are: 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 42) If the year is a century year...

  • Write a program that accepts a year and determines whether the year is a leap year....

    Write a program that accepts a year and determines whether the year is a leap year. Use the mod function. The output should be the variable extra_day, which should be 1 if the year is a leap year and 0 otherwise. The rules for determining leap years in the Gregorian calendar are as follows: All years evenly divisible by 400 are leap years. Years evenly divisible by 100 but not by 400 are not leap years. Years divisible by 4...

  • Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2:...

    Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2: Leap Years (2 Points) Your task is to write a program that takes in both a month and year (defined in the definitions file) and outputs the number of days for that month. It should take into account whether the year is a leap year or not. The resulting number of days in a given month/year combo is to be assigned to the variable...

  • In Python 3 please 3.28 LAB: Leap year A year in the modern Gregorian Calendar consists...

    In Python 3 please 3.28 LAB: 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...

  • FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary...

    FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary year or bissextile year) is a calendar year containing one additional day (or, in the case of lunisolar calendars, a month) added to keep the calendar year synchronized with the astronomical or seasonal year." In the Gregorian calendar, which is in use today, to determine leap year: A year is a leap year if it is divisible by 400 Or it is divisible by...

  • A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes...

    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 the year is a century year...

  • Suppose that today is April 1st of the year 2019. By entering your birthday, print what is a day ...

    C++ question Suppose that today is April 1st of the year 2019. By entering your birthday, print what is a day of the week you were born and how many days passed since you were born. Remember that January 1st of the year 2019 is Tuesday See the example below Read 1/1/2019 - Tue 90 Read 4/1/2019 - Mon 0 Read 4/1/2018 - Sun 365 Read 4/1/2017 -> Sat 730 Read 4/1/2016 Fri 1095 Read 4/1/2015-> Wed 1461 Bye... Wikipedia...

  • Java Program 4: Methods Purpose: To practice using both void and value-returning methods and understand when,...

    Java Program 4: Methods Purpose: To practice using both void and value-returning methods and understand when, where, and how to use them. The following problem employs the use of both void and value-returning methods: (Be sure to start with an algorithm to help you organize your ideas and provide a blueprint as to how to approach the problem. Think about what kind of return type your method will need. Carefully examine and follow ALL the program specifications (for full credit)....

  • PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program tha...

    PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program that reads a values for hour and minute from the input test file timeData.txt . The data read for valid hour and valid minute entries. Assume you are working with a 24 hour time format. Your program should use functions called validateHour and validateMinutes. Below is a sample of the format to use for your output file  timeValidation.txt :   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT