lunes, 15 de septiembre de 2025

How to know what century a year belongs to in Java

 Let's code... Let's see how to know what century a year belongs to in Java.

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

Example

For year = 1905, the output should be solution(year) = 20;

For year = 1700, the output should be solution(year) = 17.

Input/Output

[input] integer year

A positive integer, designating the year.

[output] integer

The number of the century the year is in.

Solution

    
int solution(int year) {
    
    // If year ends with 00 century is the first part
    // Example: 400 is 4. 1900 is 19.
    if (year % 100 == 0){
        return (year / 100);
    }
    else{
        // In any other case the century is that part of te year + 1
        return ((year / 100)+1);
    }  
} 
   
 

No hay comentarios.:

Publicar un comentario

Patrones de Despliegue

Un patrón de despliegue es una estrategia estructurada para actualizar o liberar nuevas versiones de software en un entorno de producción, ...