Program to find century for a year
Given a year the task is that we will find the century in the given year. The first century start from 1 to 100 and the second century start from 101 to 200 and so on .
Examples:
Input : year = 1970 Output : 20 century Input : year = 1800 Output : 18 century
CPP
// C++ code for find the century // in a given year #include <bits/stdc++.h> using namespace std; void find_century(int year) { // No negative value is allow for year if (year <= 0) cout << "0 and negative is not allow" << "for a year"; // If year is between 1 to 100 it // will come in 1st century else if (year <= 100) cout << "1st century\n"; else if (year % 100 == 0) cout << year/ 100 <<" century"; else cout << year/ 100 + 1 << " century"; } // Driven code int main() { int year = 2001; find_century(year); return 0; } |
chevron_right
filter_none
Java
// Java code for find the century // in a given year class GFG { static void find_century(int year) { // No negative value is allow for year if (year <= 0) System.out.print("0 and negative is not allow" + "for a year"); // If year is between 1 to 100 it // will come in 1st century else if (year <= 100) System.out.print("1st century\n"); else if (year % 100 == 0) System.out.print(year / 100 + " century"); else System.out.print(year / 100 + 1 + " century"); } // Driver code public static void main(String[] args) { int year = 2001; find_century(year); } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
C#
// C# code for find the century in a given year using System; class GFG { static void find_century(int year) { // No negative value is allow for year if (year <= 0) Console.WriteLine("0 and negative is not" + " allow for a year"); // If year is between 1 to 100 it // will come in 1st century else if (year <= 100) Console.WriteLine("1st century\n"); else if (year % 100 == 0) Console.WriteLine(year / 100 + " century"); else Console.WriteLine(year / 100 + 1 + " century"); } // Driver code public static void Main() { int year = 2001; find_century(year); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP code for find the century // in a given year function find_century( $year) { // No negative value is // allow for year if ($year <= 0) echo "0 and negative is not allow" , "for a year"; // If year is between 1 to 100 it // will come in 1st century else if($year <= 100) echo "1st century\n"; else if ($year % 100 == 0) echo $year / 100 ," century"; else echo floor($year / 100) + 1 , " century"; } // Driver Code $year = 2001; find_century($year); // This code is contributed by anuj_67. ?> |
chevron_right
filter_none
Output:
21 century
Recommended Posts:
- Program to check if a given year is leap year
- Python program to calculate age in year
- Find the next identical calendar year
- Print calendar for a given year in C++
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Why is programming important for first year or school students?
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find LCM of 2 numbers without using GCD
- Program to find GCD or HCF of two numbers
- Program to find LCM of two numbers
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Print the season name of the year based on the month number
- Program to find Nth term of series 2, 12, 28, 50, 77, 112, 152, 198, .....
- Program to find Nth term of series 4, 14, 28, 46, 68, 94, 124, 158, .....
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m



