Skip to content
ExamplesEspañolWorkspace →

Compound Interest

Watch your savings grow year by year.

Level
Intermediate
Topic:
Math
Concepts:
Input & output
Para
Math
Open in Codoritmo

Run, change and debug this example in your browser.

What you'll learn

A Para loop applies the interest to the running amount on each pass. Ln works out how long it takes to double your money, and Exp gives the result with continuous interest.

Code

compound-interest.psc · 20 lines
Algoritmo InteresCompuesto    Definir capital, tasa, monto Como Real    Definir anios, i Como Entero    Escribir "Capital inicial:"    Leer capital    Escribir "Interés anual (%):"    Leer tasa    Escribir "Años:"    Leer anios    monto <- capital    Para i <- 1 Hasta anios Hacer        monto <- monto * (1 + tasa / 100)        Escribir "Año ", i, ": $", Redon(monto * 100) / 100    FinPara    Escribir "Ganancia total: $", Redon((monto - capital) * 100) / 100    Si tasa > 0 Entonces        Escribir "Años para duplicar el capital: ", Redon(Ln(2) / Ln(1 + tasa / 100) * 10) / 10    FinSi    Escribir "Con interés continuo serían: $", Redon(capital * Exp(tasa / 100 * anios) * 100) / 100FinAlgoritmo

Flowchart

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

Algoritmo InteresCompuestoSíNoAlgoritmo InteresCompuestoAlgoritmo InteresCompuestoDefinir capital, tasa, monto Como RealDefinir capital, tasa, montoComo RealDefinir anios, i Como EnteroDefinir anios, i Como EnteroEscribir "Capital inicial:"Escribir "Capital inicial:"Leer capitalLeer capitalEscribir "Interés anual (%):"Escribir "Interés anual (%):"Leer tasaLeer tasaEscribir "Años:"Escribir "Años:"Leer aniosLeer aniosmonto <- capitalmonto <- capitalPara i <- 1 Hasta anios HacerPara i <- 1 Hasta aniosmonto <- monto * (1 + tasa / 100)monto <- monto * (1 + tasa /100)Escribir "Año ", i, ": $", Redon(monto * 100) / 100Escribir "Año ", i, ": $",Redon(monto * 100) / 100Escribir "Ganancia total: $", Redon((monto - capital) * 100) / 100Escribir "Ganancia total: $",Redon((monto - capital) * 100)/ 100Si tasa > 0 Entoncestasa > 0Escribir "Años para duplicar el capital: ", Redon(Ln(2) / Ln(1 + tasa / 100) * 10) / 10Escribir "Años para duplicarel capital: ", Redon(Ln(2) /Ln(1 + tasa / 100) * 10) / 10Escribir "Con interés continuo serían: $", Redon(capital * Exp(tasa / 100 * anios) * 100) / 100Escribir "Con interés continuoserían: $", Redon(capital *Exp(tasa / 100 * anios) * 100…FinAlgoritmoFinAlgoritmo

Sample run

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

Capital inicial:User answer: 10000Interés anual (%):User answer: 8Años:User answer: 5Año 1: $10800Año 2: $11664Año 3: $12597.12Año 4: $13604.89Año 5: $14693.28Ganancia total: $4693.28Años para duplicar el capital: 9Con interés continuo serían: $14918.25

Challenge

Add a fixed deposit that is added at the end of every year.

Show hint

Read the deposit before the loop and add it to monto after applying the interest.

Open in Codoritmo