#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
double seed_money, target_money, annual_interest;
printf("Input seed money : ");
scanf("%lf", &seed_money);
printf("Input target money : ");
scanf("%lf", &target_money);
printf("Input annual interest (%%) : ");
scanf("%lf", &annual_interest);
double fund = seed_money;
int year_count = 0;
while (fund < target_money)
{
fund = fund + fund * annual_interest / 100.0;
year_count = year_count + 1;
printf("Year %d, fund %f\n", year_count, fund);
}
printf("It takes %d years.\n", year_count);
}
while문에서 () 안에는 조건을 넣어주고, fund = fund + fund * annual_interest / 100.0
라는 식을 넣어주고, year_count = year_count + 1;
식을 넣어서 조건을 만족하면 루프를 벗어나게 해준다.
fund = fund + fund * annual_interest / 100.0
는 fund += fund * annual_interest
로,
year_count = year_count + 1;
는 year_count += 1;
로 줄일 수 있다.
즉, +=
는 자기 자신을 더할 경우 뒤에 쓰이는 자기 자신을 생략할 수 있다.