Algorithm problem over percentage

Asked

Viewed 448 times

0

I had to do this algorithm :

In 2010, a small Brazilian city has 20,000 inhabitants. The forecast of IBGE is that this city grows at a rate of 5% per year. Knowing of this, make an algorithm that prints on the screen the year and the population planned for the city in that year, with the year ranging from 2011 to 2030.

using the structure for and I made this algorithm:

float populacao;
int conta,taxa,total;

populacao=20.000;
taxa=1.05;
for (conta=2011;conta<=2030;conta++)
{
    total=populacao*taxa;
    populacao=total;

    printf("O ano de %d tera %f de habitantes \n",conta,populacao);
}

but the result of the population is giving 20.000000for all the years,?

2 answers

4


Your rate and total are defined as int, but she’s a float. Change this and your algorithm will be working perfectly.

float populacao,taxa,total;
int conta;

populacao=20.000;
taxa=1.05;
for (conta=2011;conta<=2030;conta++)
{
    total=populacao*taxa;
    populacao=total;

    printf("O ano de %d tera %f de habitantes \n",conta,populacao);
}

0

#include <stdio.h>
int main(void)
{
    float populacao,taxa,total;
    int conta;

    populacao = 20.000;
    taxa = 0.05;
    for (conta=2011; conta<=2030; conta++)
    {
       total = populacao + (populacao * taxa);
       populacao = total;
       printf("O ano de %i tera %f de habitantes \n",conta,populacao);
    } 
    return(0);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.