Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Determine if a year is a leap year or not.
- #include <iostream>
- #include <string>
- using namespace std;
- // This function returns true if a
- // year is a leap year, otherwise false.
- int is_leap(int year)
- {
- if (year % 100 == 0) // If it's divisible by 100,
- {
- if (year % 400 == 0) // And divisible by 400.
- {
- return true; // It's a leap year.
- }
- else // If it's divisible by 100 but not 400,
- {
- return false; // It's not leap year.
- }
- }
- else // Else if it's not divisible by 100,
- {
- if (year % 4 == 0) // If it's divisible by 4,
- {
- return true; // It's a leap year.
- }
- else // Else if it's not divisible by 4,
- {
- return false; // It's not a leap year.
- }
- }
- }
- int main()
- {
- bool successful_conversion = false; // Is the user's input an integer?
- bool is_a_leap_year = false; // Is the year a leap year?
- string year; // User's input.
- int converted_srting_input = 0; // User's input converted to an integer.
- cout << "This program will determine if a year is a leap year.\n";
- while (!successful_conversion) // While the user's input is not an integer,
- {
- cout << "\nWhat year do you want to check? ";
- cin >> year; // Get user input.
- try // Prepare to handle if the user's input is not an integer.
- {
- converted_srting_input = stoi(year); // Try to convert input to integer.
- successful_conversion = true; // Input was an integer.
- }
- catch (...) // If the string input can't be converted to an integer,
- {
- cout << "Something is wrong with the year you entered.\n"
- "Try again.\n\n";
- }
- }
- is_a_leap_year = is_leap(converted_srting_input); // Determine if it's a leap year.
- // Announce the result.
- cout << "\nThe year " << converted_srting_input << " is ";
- if (!is_a_leap_year)
- {
- cout << "not ";
- }
- cout << "a leap year.\n";
- return 0; // End the program.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement