Skip to content
ExamplesEspañolWorkspace →

Leap Year

Applies the calendar rules to tell whether a year is a leap year.

Level
Intermediate
Topic:
Decisions
Concepts:
Input & output
Si
Colloquial conditions
Open in Codoritmo

Run, change and debug this example in your browser.

What you'll learn

Es Multiplo De is a colloquial condition, a more readable way to write MOD = 0. The order of the nested Si blocks matters: 400 first, then 100, then 4.

Code

leap-year.psc · 19 lines
Algoritmo AnioBisiesto    Definir anio Como Entero    Escribir "Escribe un año:"    Leer anio    // "Es Multiplo De" es una condición coloquial de PSeInt    Si anio Es Multiplo De 400 Entonces        Escribir anio, " es bisiesto."    SiNo        Si anio Es Multiplo De 100 Entonces            Escribir anio, " no es bisiesto (es múltiplo de 100 pero no de 400)."        SiNo            Si anio Es Multiplo De 4 Entonces                Escribir anio, " es bisiesto: febrero tiene 29 días."            SiNo                Escribir anio, " no es bisiesto."            FinSi        FinSi    FinSiFinAlgoritmo

Flowchart

Each block in the flowchart matches one instruction in the code.

Algoritmo AnioBisiestoSíNoSíNoSíNoAlgoritmo AnioBisiestoAlgoritmo AnioBisiestoDefinir anio Como EnteroDefinir anio Como EnteroEscribir "Escribe un año:"Escribir "Escribe un año:"Leer anioLeer anioSi anio MOD 400 = 0 Entoncesanio MOD 400 = 0Escribir anio, " es bisiesto."Escribir anio, " es bisiesto."Si anio MOD 100 = 0 Entoncesanio MOD 100 = 0Escribir anio, " no es bisiesto (es múltiplo de 100 pero no de 400)."Escribir anio, " no esbisiesto (es múltiplo de 100pero no de 400)."Si anio MOD 4 = 0 Entoncesanio MOD 4 = 0Escribir anio, " es bisiesto: febrero tiene 29 días."Escribir anio, " es bisiesto:febrero tiene 29 días."Escribir anio, " no es bisiesto."Escribir anio, " no esbisiesto."FinAlgoritmoFinAlgoritmo

Sample run

This is what the program shows when it runs. The answers typed by the user are highlighted.

Escribe un año:User answer: 20242024 es bisiesto: febrero tiene 29 días.

Challenge

Ask for a start and an end year, and list every leap year in between.

Show hint

Wrap the decision in a Para loop that walks through the years in the range.

Open in Codoritmo
  • Body Mass Index

    Computes your BMI and classifies it as underweight, normal and so on.

    Intermediate
  • Can I Vote?

    Tells you whether you can vote or how many years you have left.

    Beginner
  • Guess the Number

    Guess a secret number from 1 to 100 in seven tries.

    Intermediate