반응형

복리는 이자 계산 중 하나인데 일정 기간마다 원금에 이자를 더한 것을 새로운 원금으로 하여 계산하는 방법이다.


이를 씨언어로 표현하면

#include <stdio.h>

int main(void)

{

double total, rate, investment;  //(총금, 이율, 원금)

int year, n;    //(돈 맡기는 기간, 출력 년도)

n =1;

printf("원금을 입력하세요");

scanf("%lf", &investment);

printf("이율을 입력하세요");

scanf("%lf", &rate);

printf("기간(년)을 입력하세요");

scanf("%d", &year);

total = investment;

rate = rate / 100.0; 

printf("연도 원리금\n");

while(year>0)

{

total = total *(1+rate);

printf("%d %lf\n",n, total);

year--;

n++;

}

return 0;


}

출력값



for문으로도 충분히 나타낼 수 있다.

반응형
반응형

이 알고리즘은 파이 값을 계산 하는 알고리즘이다. 

가 된다.

따라서 이를 알고리즘으로 작성하면

#include <stdio.h>  //헤더파일

int main(void)

{

double w, x, y, z, pii;  // w,x,y,z 는 각각 분자와 분모 숫자 pii는 pi값 

int count;

w = 4.0;

x = 2.0;

y = 3.0;

z = 4.0;

pii = 3.0;


printf("루프카운트를입력하세요");

scanf("%d", &count);

while(count>0) // count가 0보다작아지면 반복이 끝난다.

{

pii = pii + (w/(x*y*z));

w = w*(-1.0);

x = x+2.0;

y = y+2.0;

z = z+2.0;

count--;

}

printf("파이값은 %lf 입니다\n", pii);

return 0;

}


가 되고 출력값은

루프카운트를입력하세요9999999999

파이값은 3.141593 입니다.

가 된다.



반응형

+ Recent posts