运行结果:

C语言代码:
/* c how to program 习题4.15 ** 计算利息 */ #include <stdio.h> #include <math.h> // function main begins program execution int main( void ) { //定义principal为本金,rate为利率 double principal = 1000.0; int rate; //利用循环计算不同利率的利息 for(rate = 5; rate <= 10; rate++){ //打印当前利率 printf("Interest Rate:%lf\n", rate/100.0); //打印列表格式 printf("%4s%21s\n", "Year", "Amount on deposit"); //利用循环计算1=10的利息 for(unsigned int year = 1; year <= 10; ++year){ //计算利息 double amount = principal * pow(1.0 + rate/100.0, year); //按格式输出结果 printf("%4u%21.2f\n", year, amount); } //换行 puts(""); } }