﻿ C++ Exercises: Convert a given year to century - w3resource

# C++ Exercises: Convert a given year to century

## C++ Date: Exercise-3 with Solution

Write a C++ program to convert a given year to a century.

Note: A century is a period of 100 years. Centuries are numbered ordinally in English and many other languages. The word century comes from the Latin centum, meaning one hundred. Century is sometimes abbreviated as c.

Sample Solution-1:

C++ Code:

``````#include <iostream>
#include <cmath> // Including the cmath library for mathematical functions

using namespace std; // Using the standard namespace

// Function to determine the century of a given year
double year_to_century(int y) {
return floor((y - 1) / 100 + 1); // Calculate the century by dividing the year by 100 and rounding down to the nearest integer
}

// Main function
int main() {
int y; // Declaring an integer variable to store the year

y = 1900; // Assigning a year value
cout << "Year: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 1999; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 2000; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 2010; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 2020; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

return 0; // Return 0 to indicate successful execution
}
``````

Sample Output:

```Year: 1900,  Century of the year: 19
Year: 1999,  Century of the year: 20
Year: 2000,  Century of the year: 20
Year: 2010,  Century of the year: 21
Year: 2020,  Century of the year: 21
```

Flowchart:

Sample Solution-2:

C++ Code:

``````#include <iostream> // Including the input/output stream library

using namespace std; // Using the standard namespace

// Function to determine the century of a given year
double year_to_century(int y) {
return (y - 1) / 100 + 1; // Calculating the century by dividing the year by 100 and adding 1
}

// Main function
int main() {
int y; // Declaring an integer variable to store the year

y = 1900; // Assigning a year value
cout << "Year: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 1999; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 2000; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 2010; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

y = 2020; // Assigning a year value
cout << "\nYear: " << y << ",  Century of the year: " << year_to_century(y); // Display the year and its corresponding century

return 0; // Return 0 to indicate successful execution
}
``````

Sample Output:

```Year: 1900,  Century of the year: 19
Year: 1999,  Century of the year: 20
Year: 2000,  Century of the year: 20
Year: 2010,  Century of the year: 21
Year: 2020,  Century of the year: 21
```

Flowchart:

C++ Code Editor: