# cómo obtener un valor al hacer un bucle en FORTRAN

Hello guys! There is a population of say, 120 million, which increases by 8% every year. I want to have a DO loop starting from 1990 to 2020 to state the year the population exceeds 125 million. Pseudocode or Fortran code will be appreciated.

preguntado el 05 de mayo de 13 a las 14:05

Tú sabes cualquier programming languages?? -

## 3 Respuestas

This is not a problem for which loops are either necessary or appropriate. The simple equation

``````num_years = log(125.0/120.0)/log(1.08)
``````

(which evaluates to approximately `0.53`) is all that is necessary. This is a straightforward rewriting of the formula for compound interest calculations, that is

``````compound_amt = initial_amt * (1+interest_rate)**num_years
``````

con, en este caso, `initial_amt = 120*10**6`, `compound_amt = 125*10**6` y `interest_rate = 8%`.

contestado el 05 de mayo de 13 a las 22:05

Its easy to find tutorials for loops in Fortran that solves your problems. See here for ejemplo. But generally, you want something like this:

``````sum=120e6
startyear=1990
do i = 1,30
sum = sum + sum*8./100.
if sum > 125e6 then
write(*,*), "Year ", i+startyear, " population exceeded ", sum
end if
end do
``````

contestado el 05 de mayo de 13 a las 14:05

``````population = 120000.0
year = 1990
loop:
population = population + (population * 0.08)
year = year + 1
if (population > 125000.0) go to print_done
if (year > 2020) go to print_not_found
go to loop

print_done:
print "The population was " population " in the year " year
stop

print_not_found:
print "Searched to year " year " and the population only reached " population
stop
``````

Note that there's an issue as to whether the year should be incremented before or after the population is checked. This depends on whether you want the population at the beginning of the year or the end of the year (assuming the initial value was at the beginning of the year).

contestado el 05 de mayo de 13 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.