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 starts 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
Below is the Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
void find_century( int year)
{
if (year <= 0)
cout << "0 and negative is not allow"
<< "for a year" ;
else if (year <= 100)
cout << "1st century\n" ;
else if (year % 100 == 0)
cout << year/ 100 << " century" ;
else
cout << year/ 100 + 1 << " century" ;
}
int main()
{
int year = 2001;
find_century(year);
return 0;
}
|
Java
class GFG {
static void find_century( int year) {
if (year <= 0 )
System.out.print( "0 and negative is not allow"
+ "for a year" );
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" );
}
public static void main(String[] args) {
int year = 2001 ;
find_century(year);
}
}
|
Python3
def find_century(year):
if (year < = 0 ):
print ( "0 and negative is not allow for a year" )
elif (year < = 100 ):
print ( "1st century" )
elif (year % 100 = = 0 ):
print (year / / 100 , "century" )
else :
print (year / / 100 + 1 , "century" )
year = 2001
find_century(year)
|
C#
using System;
class GFG {
static void find_century( int year) {
if (year <= 0)
Console.WriteLine( "0 and negative is not"
+ " allow for a year" );
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" );
}
public static void Main() {
int year = 2001;
find_century(year);
}
}
|
PHP
<?php
function find_century( $year )
{
if ( $year <= 0)
echo "0 and negative is not allow"
, "for a year" ;
else if ( $year <= 100)
echo "1st century\n" ;
else if ( $year % 100 == 0)
echo $year / 100 , " century" ;
else
echo floor ( $year / 100) + 1
, " century" ;
}
$year = 2001;
find_century( $year );
?>
|
Javascript
<script>
function find_century( year) {
if (year <= 0)
document.write( "0 and negative is not allow"
+ "for a year" );
else if (year <= 100)
document.write( "1st century\n" );
else if (year % 100 == 0)
document.write(parseInt(year / 100) + " century" );
else
document.write(parseInt(year / 100) + 1 + " century" );
}
let year = 2001;
find_century(year);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...