C Primer Plus 练习 6-15

来源:互联网 发布:ks.js.cn 编辑:程序博客网 时间:2024/06/05 11:07

题目:Daphne以10%的单利息投资了100美元。Deirdre以每年5%的复利息投资了100美元。编写一个程序,计算需要多少年Deirdre的投资额才会超过Daphne,并显示出到那时两个人的投资额。

//6-15 #include<stdio.h>#define CAPITAL 100#define RATE_DA 0.1#define RATE_DE 0.05int main(void){ int year;float money_Da=CAPITAL;float money_De=CAPITAL;for(year=0;money_De<=money_Da;year++){money_Da+=CAPITAL*RATE_DA;money_De*=1+RATE_DE;}printf("%d\n",year);printf("Daphne will have %.2f $.\n",money_Da);printf("Deirdre will have %.2f $.\n",money_De);return 0;}


0 0
原创粉丝点击