5.4 곱하기 연산자

공기훈·2021년 8월 16일
0

홍정모의 따배씨

목록 보기
17/49

복리 계산 프로그램

#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.0fund += fund * annual_interest로,

year_count = year_count + 1;year_count += 1;로 줄일 수 있다.
즉, +=는 자기 자신을 더할 경우 뒤에 쓰이는 자기 자신을 생략할 수 있다.

profile
be a coding master

0개의 댓글