Programa para encontrar el siglo para un año.

Dado un año, la tarea es encontrar el siglo en el año dado. El primer siglo comienza del 1 al 100 y el segundo siglo comienza del 101 al 200 y así sucesivamente.

Ejemplos:  

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;
}

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.

Python3

# Python3 code for find the century
# in a given year
 
def find_century(year):
   
    # No negative value is allow for year
    if (year <= 0):
        print("0 and negative is not allow for a year")
         
    # If year is between 1 to 100 it
    # will come in 1st century
    elif (year <= 100):
        print("1st century")
    elif (year % 100 == 0):
        print(year // 100,"century")
    else:
        print(year // 100 + 1,"century")
 
# Driver code
year = 2001
find_century(year)
 
# This code is contributed by shubhamsingh10

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.

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.
?>

Javascript

<script>
 
 
// Java Script code for find the century
// in a given year
function find_century( year) {
 
    // No negative value is allow for year
    if (year <= 0)
    document.write("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)
        document.write("1st century\n");
 
    else if (year % 100 == 0)
        document.write(parseInt(year / 100) + " century");
 
    else
        document.write(parseInt(year / 100) + 1 + " century");
}
 
// Driver code
 
    let year = 2001;
    find_century(year);
 
//contributed by sravan kumar
</script>

Producción: 

21 century

Publicación traducida automáticamente

Artículo escrito por SHARIQ_JMI y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *