Question

In: Computer Science

C++ questions, please make sure to dividet he program into functions which perform each major task,...

C++ questions, please make sure to dividet he program into functions which perform each major task,

  1. A leap year is defined as any calendar year which meets the following criteria:

    • If the year is not divisible by 4, it is a common year

    • If the year is not divisible by 100, it is a leap year

    • If the year is not divisible by 400, it is a common year

    • Otherwise it is a leap year
      For your program you will take in three integers from the user:

    • A four digit year

    • The day of the month (between 1 and 31)

    • The month (between 1 and 12, 1 representing January and 12 representing December)

      Your program will output whether or not the date entered is a valid calendar date. Be sure to divide your program into functions which perform each major task.

      Hint: There are 30 days in September, April, June and November. If the year is a leap year, there are 29 days in February. If the year is a common year there are 28 days in February. All other months have 31 days. Some examples to help you verify leap year calculation:

  2. Condition

    Result

    Examples

    Not divisible by 4

    Not a leap year

    2009, 2010, 2011

    Divisible by 4

    Leap year

    2008, 2012, 2016

    Divisible by 100

    Not a leap year

    1800, 1900, 2100

    Divisible by 400

    Leap year

    2000, 2400

Solutions

Expert Solution

#include <iostream>
using namespace std;

void leapyear(int year) {
    //Giving conditions for each year dividing by 4,100 and 400
    if (year % 4 == 0)
    {
        if (year % 100 == 0)
        {
            if (year % 400 == 0)
                cout << year << " is a leap year.";
            else
                cout << year << " is not a leap year.";
        }
        else
            cout << year << " is a leap year.";
    }
    else
        cout << year << " is not a leap year.";
}

int main()
{
    int year;
    //Taking user input
    cout << "Enter a year: ";
    cin >> year;
    //Calling leap year function
    leapyear(year);
    return 0;
}

Sample input and output:

Enter a year: 2010
2010 is not a leap year.

Enter a year: 2016
2016 is a leap year.

#include <iostream>
using namespace std;

void leapyear(int year) {
    //Giving conditions for each year dividing by 4,100 and 400
    if (year % 4 == 0)
    {
        cout << year << " is divided by 4.\n";
        if (year % 100 == 0)
        {
            cout << year << " is divided by 100.\n";
            if (year % 400 == 0) {
                cout << year << " is divided by 400.\n";
                cout << year << " is a leap year.";
            }
            else
                cout << year << " is not a leap year.";
        }
        else
            cout << year << " is a leap year.";
    }
    else {
        cout << "Not divisible by 4.\n";
        cout << year << " is not a leap year.";
    }
}

int main()
{
    int year;
    //Taking user input
    cout << "Enter a year: ";
    cin >> year;
    //Calling leap year function
    leapyear(year);
    return 0;
}

Sample input and output:

Enter a year: 2009
Not divisible by 4.
2009 is not a leap year.

Enter a year: 1800
1800 is divided by 4.
1800 is divided by 100.
1800 is not a leap year.


Related Solutions

C++ questions, Please make sure to divide your program into functions which perform each major task....
C++ questions, Please make sure to divide your program into functions which perform each major task. The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic: ROCK defeats SCISSORS (“smashes”) PAPER defeats ROCK (“wraps”) SCISSORS defeats PAPER (“slices”) If both players choose the same selection the game ends in a tie. Write a program that asks...
C++ questions about Monty Hall Problem, please make sure to divide the program into functions which...
C++ questions about Monty Hall Problem, please make sure to divide the program into functions which perform each major task. Imagine yourself on the set of a game show. You're given the choice of three doors. Behind one of the doors is a car you can drive home in if you guess correctly. Behind the other two doors are goats. After you initially guess the door, the host of the game show (who knows the door holding the car) opens...
C++ program to perform each of the area calculations in separate functions. Your program will take...
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
Please design a PLC program to perform the following task: An LED will be on when...
Please design a PLC program to perform the following task: An LED will be on when it’s activated by an NO push button for an accumulated 6 seconds. In other words, the push button can be on and off, but when it’s accumulated for six seconds, the LED will be on. After six seconds, the LED will be on for four seconds and is then reset itself for another cycle. Post LogixPro image of this programming Cascading timer Assume the...
1. For each of the following, write C++ statements that perform the specified task. Assume that...
1. For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in four bytes and that the starting address of the built-in array is at location 1002500 in memory. Declare an unsigned int built-in array values with five elements initialized to the even integers from 2 to 10. Assume that the constant size has been defined as 5. Declare a pointer vPtr that points to an object of type unsigned int....
Please answer this using MATLAB (Not c or c++) and make sure to provide proper commenting:...
Please answer this using MATLAB (Not c or c++) and make sure to provide proper commenting: a. Write a function that writes a series of random Fahrenheit temperatures and their corresponding Celsius temperatures to a tab-delimited le. Use 32 to 212 as your temperature range. From the user, obtain the following: 1) The number of temperatures to randomly generate. 2) The name of the output file. b. Write a function that reads a file produced by part (a). Focusing only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT