# c語言 複利（請幫我除錯）

1.計算出每年年底存款總額：年複利、月複利、日複利，皆須列出。

1.你不可以使用任何內建數學函式

2.使用 for loop，你大約須使用3個for loop，一大loop包住兩小loop(巢狀loop)

3.在printf中使用 %15.0f來控制輸出。

Note: Proper indentation should be used.

Proper internal documentation should be included.

hint: 月複利的利率為 0.02 / 12，日複利的利率為 0.02 / 365

Sample output

Year Deposit (dayily) Deposit (monthly)Deposit (yearly)

1102020元(365)102018元(12)102000元(1)

2104081元(730)104078元(24)104040元(2)

3106184元(1095)106178元(36)106121元(3)

……………..

……………..

10122140元(3650)122120元(120)121899元(10)

-----------------------------------------------------------------------

#include <stdio.h>

int main(void)

{

int year=1,month=1,day=1;

float b=0.02,c=0.02/12,d=0.02/365,yearly=102000,monthly=102018,dayily=102020;

printf(" Year Deposit (dayily) Deposit (monthly) Deposit (yearly)\n");

for(year,yearly,b; year<=10 ; year++,yearly=yearly*b+yearly)

{

for(month,monthly,c;month<=12 ;month++,monthly=monthly*c+monthly)

for(day,dayily,d;day<=365 ;day++,dayily=dayily*d+dayily)

printf("%d %.0f %.0f %.0f\n",year,dayily,monthly,yearly);

}

getchar();

return 0;

}

---------------------------------------------------------------------------------------------

for(year,yearly,b; year<=10 ; year++,yearly=yearly*b+yearly)

（這一句是指年利率）

for(month,monthly,c;month<=12 ;month++,monthly=monthly*c+monthly)

（這一句是指月利率）

for(day,dayily,d;day<=365 ;day++,dayily=dayily*d+dayily)

（這一句是指日利率）

Rating
• ?
Lv 4

你寫的不錯,快接近答案了.

不過

for(month....)

for(day....)

printf("%d %15.0f .....);

前兩行要加 ; 否則會被算到 for loop 而一直壘加.

另外不建議在for的括號內加入太多的運算式, 畢竟可讀性比你省打幾行字還重要.

#include <stdio.h>

int main(void)

{

int year, month, day;

float b=0.02,c=0.02/12,d=0.02/365,yearly=100000,monthly=100000,dayily=100000;

printf(" Year Deposit (dayily) Deposit (monthly) Deposit (yearly)\n");

for(year = 1; year<=10 ; year++)

{

yearly += yearly*b;

for(month = 1; month<=12 ;month++)

monthly += monthly*c;

for(day = 1; day<=365 ;day++)

dayily += dayily*d;

printf("%4d %15.0f %15.0f %15.0f\n",year,dayily,monthly,yearly);

}

getchar();

return 0;

}

• Anonymous
6 years ago

到下面的網址看看吧