正在加载图片...
用函数计算利息: #include<stdio.h 方米函数功能:根据本金,年份和利率计算利息*/ hi fFin float Interest(float capital, int year, float rate) #inc voic ireturn(capital year*12*rate) void [int void main( ino [int pri tint year float capital, interest prin Sc prin Itf("please input capital and year: " ) sw scanf(e“%f%d",& capital, &year):/输入数据"/ ify icc switch(year)⌒下面根据不同年限和不同利率调用函数吻 ct case 1: interest=Interest(capital, year, 0.0063): break if(y ct case 2: interest=Interest(capital, year, 0.0066), break ct case 3 if(y ce case 4: interest=Interest(capital, year, O0069); break; cl case 5: ifo ci case 6: li d case 7: interest=Interest(capital, year, 0.0075); break 3 default: interest=Interest(capital, year, 0.0085): break [i prl prin: /1 printf("the sum of capital and interest is %f", capital+interest) 最店覆广最后输出计算结果3 作业答案 ▪ 习题4.9 ▪ 思路:根据输入的year得到不同的利率,由此求 出不同的利息,再输出。 ▪ 方法一:用if-else ▪ 方法二:用switch() ▪ 进一步,可以使用函数。 ▪ } else /*是 0*/ { printf("%d is 0 \n", m);} } if (m % 2 == 0) /*是否为偶数*/ { printf("%d is even\n", m); } /*是偶数*/ else { printf("%d is odd\n", m); } /*是奇数*/ } 方法一: #include<stdio.h> void main() {int year;float capital,interest; printf(“please input capital and year:”); scanf(“%f,%d”,&capital,&year); /*输入数据*/ if(year==1) /*下面的几个分支分别根据不同年限计算利息*/ {interest=year*12*0.0063*capital; } if(year==2) {interest=year*12* 0.0066* capital; } if(year==3|| year==4) {interest=year*12* 0.0069* capital; } if(year>=5&& year<=7) {interest=year*12* 0.0075* capital; } if(year>=8) {interest=year*12* 0.0085* capital; } printf(“the sum of capital and interest is :%f”,capital+interest); /*最后输出计算结果*/ } 方法二: #include<stdio.h> void main() {int year;float capital,interest; printf(“please input capital and year:”); scanf(“%f,%d”,&capital,&year); /*输入数据*/ switch(year) /*下面的几个分支分别根据不同年限计算利息*/ {case 1:interest=year*12*0.0063*capital; break; case 2:interest=year*12*0.0066*capital; break; case 3: case 4:interest=year*12*0.0069*capital; break; case 5: case 6: case 7:interest=year*12*0.0075*capital; break; default:interest=year*12*0.0085*capital; break; } printf(“the sum of capital and interest is :%f”,capital+interest); /*最后输出计算结果*/ } 用函数计算利息: #include<stdio.h> /*函数功能:根据本金,年份和利率计算利息*/ float Interest(float capital, int year, float rate) {return (capital*year*12*rate);} void main() {int year;float capital,interest; printf(“please input capital and year:”); scanf(“%f,%d”,&capital,&year); /*输入数据*/ switch(year) /*下面根据不同年限和不同利率调用函数*/ {case 1: interest=Interest(capital,year,0.0063); break; case 2: interest=Interest(capital,year,0.0066); break; case 3: case 4: interest=Interest(capital,year,0.0069); break; case 5: case 6: case 7: interest=Interest(capital,year,0.0075); break; default: interest=Interest(capital,year,0.0085); break; } printf(“the sum of capital and interest is :%f”,capital+interest); /*最后输出计算结果*/ }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有