循环结构实例009

设某县2000 年工业总产值为200 亿元, 如果该县预计平均年工业总产值增长率为4. 5%, 那么多少年后该县年工业总产值将超500 亿元。

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
void main()
{
    int year=0;
    float value=200;
    while(value<=500)
    {
        value = value * (1 + 0.045);
        year++;
    }
    printf("At the end of %d years, the total industial of output value will exceed 50 billion.", year);
}

方法二:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
void main ()
{
    int year=0;
    float value;
    for(value=200; value<=500;)
    {
        year++;
        value = value*(1+0.045);
    }
    printf("At the end of %d years, the total industial of output value will exceed 50 billion.", year);
}