C++ 如何编写 计算 单利与复利的测试程序代码?

2025-10-27 13:11:36

1、新建一个.CPP文件,导入对应头文件,使用命名空间。

2、#include<iostream>

using namespace std;

int main()

{

int bj=0;//本金

int year;//投资年限

float sumusd=0;//总收益======变量一定要记得初始化,否则会让自己蒙圈。配有未初始化的错误截图

using namespace std;//使用命名空间

for(int i=100,j=0;i*0.05<=10;i+=5,j++)

{

cout<<" Cleo " << i <<" 美元投资"<<j<<"年,收益 "<< i*0.05 <<" 美元  "<<endl;

cout<<"Daphne 100 美元 投资 每年收益 都是 10美元!"<<endl;

bj=i;//本金

year=j; //年

sumusd+=i*0.05; //cout<<sumusd<<endl; 测试结果

}

cout<< "Cleo 投资" <<year<< "年后,Cleo 的年收益才能超过Daphne 的年收益。\n Cleo "<<year <<" 年累计收益为 "<<sumusd<<endl;

cout<< "Daphne投资" <<year<< "年后,累计收益为 。"<< year*10<<endl;;

return 0;

}

3、运行代码即可。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢