0316复利和单利计算更新4

  1 #include<stdio.h>
  2 #include<math.h>
  3 int year,n;
  4 double p,q,F=0.0;
  5 void innit()
  6 {
  7     printf("注释:p为现金
");
  8     printf(" year为年限
");
  9     printf(" n为年复利次数
");
 10     printf(" q为年利率
");
 11     printf(" F为年复利终值
");
 12 }
 13 void menu()
 14 {
 15     printf("	|------------------------------------|
");
 16     printf("	|               welcome              |
");
 17     printf("	|------------------------------------|
");
 18     printf("	|          1、复利计算               |
");
 19     printf("	|          2、单利计算               |
");
 20     printf("	|          0、退出系统               |
");
 21     printf("	|------------------------------------|
");
 22 }
 23 //复利计算
 24 void compoundinterest(){
 25     int I;
 26     int ch;
 27 B:printf("	1、获取复利终值
");
 28   printf("	2、根据复利终值获取本金
");
 29   printf("	3、估算获得年终值的年限
");
 30   printf("	4、估算获得年终值的报酬率
");
 31   printf("请选择功能<1~4>:");
 32   scanf("%d",&I);
 33   if(I==1)
 34   {
 35       printf("请输入存款金额:");
 36       scanf("%lf",&p);
 37       printf("请输入存入存蓄年限:");
 38       scanf("%d",&year);
 39       printf("请输入年复利次数:");
 40       scanf("%d",&n);
 41       printf("请输入年利率:");
 42       scanf("%lf",&q);
 43       F=p*pow((1+q/n),year*n);
 44       printf("复利终值为%lf:",F);
 45   }
 46   if(I==2)
 47   {
 48       printf("请输入复利终值:");
 49       scanf("%lf",&F);
 50       printf("请输入存入存蓄年限:");
 51       scanf("%d",&year);
 52       printf("请输入年复利次数:");
 53       scanf("%d",&n);
 54       printf("请输入年利率:");
 55       scanf("%lf",&q);
 56       p=F/(pow((1+q/n),year*n));
 57       printf("复利本金为%lf:",p);
 58   }
 59   if(I==3)
 60   {
 61           printf("请输入存款金额:");
 62       scanf("%lf",&p);
 63       printf("请输入年利率:");
 64       scanf("%lf",&q);
 65       printf("请输入年复利次数:");
 66       scanf("%d",&n);
 67       printf("请输入年终值:");
 68           scanf("%lf",&F);
 69 
 70      year=(int)(log(F/p)/log(1+q/n))/n;
 71      // F=p*(1+q*year);
 72       printf("年限为%d
:",year);
 73 
 74 
 75   }
 76   if(I==4)
 77   {
 78       printf("请输入存款金额:");
 79       scanf("%lf",&p);
 80       printf("请输入存入存蓄年限:");
 81       scanf("%d",&year);
 82       printf("请输入年复利次数:");
 83       scanf("%d",&n);
 84        printf("请输入年终值:");
 85           scanf("%lf",&F);
 86       //F=p*pow((1+q/n),year*n);
 87           q=n*(pow(F/p,1.0/(year*n))-1);
 88       printf("复利的年利率为%lf:
",q);
 89 
 90   }
 91   printf("
	是否要重新计算?(1/0)
");
 92   scanf("%d",&ch);
 93   if(ch==1)
 94   {
 95       goto B;
 96   }
 97 }
 98 
 99 //单利计算
100 void simpleinterest()
101 {
102     int n;
103     int ch;
104 A:printf("	1、获取单利终值
");
105   printf("	2、根据单利终值获取本金
");
106   printf("	3、估算获得年终值的年限
");
107   printf("	4、估算获得年终值的报酬率
");
108   printf("请选择功能<1~4>:");
109   scanf("%d",&n);
110   if(n==1)
111   {
112       
113       printf("请输入存款金额:");
114       scanf("%lf",&p);
115       printf("请输入存入存蓄年限:");
116       scanf("%d",&year);
117       printf("请输入年利率:");
118       scanf("%lf",&q);
119       
120       F=p*(1+q*year);
121       printf("单利终值为%lf
:",F);}
122   if(n==2)
123   {
124       printf("请输入单复利终值:");
125       scanf("%lf",&F);
126       printf("请输入存入存蓄年限:");
127       scanf("%d",&year);
128       printf("请输入年利率:");
129       scanf("%lf",&q);
130       
131       p=F/(1+q*year);
132       printf("本金为%lf
:",p);}
133   if(n==3){
134 
135           printf("请输入存款金额:");
136       scanf("%lf",&p);
137       printf("请输入年利率:");
138       scanf("%lf",&q);
139       
140       printf("请输入年终值:");
141           scanf("%lf",&F);
142 
143      year=(int)((F/p-1)/q);
144      // F=p*(1+q*year);
145       printf("年限为%d
:",year);
146 
147   }
148   if(n==4){
149       printf("请输入存款金额:");
150       scanf("%lf",&p);
151       printf("请输入存入存蓄年限:");
152       scanf("%d",&year);
153        printf("请输入年终值:");
154           scanf("%lf",&F);
155       //F=p*pow((1+q/n),year*n);
156           q=(F/p-1)/year;
157       printf("单利的年利率为%lf:
",q);
158 
159 
160   }
161   printf("
	是否要重新计算?(1/0)
");
162   scanf("%d",&ch);
163   if(ch==1)
164   {
165       goto A;
166 }}
167 
168 main(){
169     int ch,n;
170     while(1){
171         if(n==0) break;
172         innit();
173         menu();
174         printf("please chose<0~4>:");
175         scanf("%d",&ch);
176         switch(ch){
177         case 1:compoundinterest();break;
178         case 2:simpleinterest();break;
179         case 0:n=0;break;
180 }}}
运行结果:
1/复利计算——算年限和年利率


2、单利计算年限和年利率

6.如果每年都将积蓄的3万元进行投资,每年都能获得3%的回报,然后将这些本利之和连同年金再投入新一轮的投资,那么,30年后资产总值将变为多少?如果换成每月定投3000呢?

(定额定投收益计算办法)

  1 #include<stdio.h>
  2 #include<math.h>
  3 int year,n;
  4 double p,q,F=0.0;
  5 void innit()
  6 {
  7     printf("注释:p为现金
");
  8     printf(" year为年限
");
  9     printf(" n为年复利次数
");
 10     printf(" q为年利率
");
 11     printf(" F为年复利终值
");
 12 }
 13 void menu()
 14 {
 15     printf("	|------------------------------------|
");
 16     printf("	|               welcome              |
");
 17     printf("	|------------------------------------|
");
 18     printf("	|          1、复利计算               |
");
 19     printf("	|          2、单利计算               |
");
 20     printf("	|          0、退出系统               |
");
 21     printf("	|------------------------------------|
");
 22 }
 23 //复利计算
 24 void compoundinterest(){
 25     int I,a;
 26     int ch;
 27 B:printf("	1、获取复利终值
");
 28   printf("	2、根据复利终值获取本金
");
 29   printf("	3、估算获得年终值的年限
");
 30   printf("	4、估算获得年终值的报酬率
");
 31   printf("	5、投资获利
");
 32   printf("请选择功能<1~5>:");
 33   scanf("%d",&I);
 34   if(I==1)
 35   {
 36       printf("请输入存款金额:");
 37       scanf("%lf",&p);
 38       printf("请输入存入存蓄年限:");
 39       scanf("%d",&year);
 40       printf("请输入年复利次数:");
 41       scanf("%d",&n);
 42       printf("请输入年利率:");
 43       scanf("%lf",&q);
 44       F=p*pow((1+q/n),year*n);
 45       printf("复利终值为%lf:",F);
 46   }
 47   if(I==2)
 48   {
 49       printf("请输入复利终值:");
 50       scanf("%lf",&F);
 51       printf("请输入存入存蓄年限:");
 52       scanf("%d",&year);
 53       printf("请输入年复利次数:");
 54       scanf("%d",&n);
 55       printf("请输入年利率:");
 56       scanf("%lf",&q);
 57       p=F/(pow((1+q/n),year*n));
 58       printf("复利本金为%lf:",p);
 59   }
 60   if(I==3)
 61   {
 62           printf("请输入存款金额:");
 63       scanf("%lf",&p);
 64       printf("请输入年利率:");
 65       scanf("%lf",&q);
 66       printf("请输入年复利次数:");
 67       scanf("%d",&n);
 68       printf("请输入年终值:");
 69           scanf("%lf",&F);
 70 
 71      year=(int)(log(F/p)/log(1+q/n))/n;
 72      // F=p*(1+q*year);
 73       printf("年限为%d
:",year);
 74 
 75 
 76   }
 77   if(I==4)
 78   {
 79       printf("请输入存款金额:");
 80       scanf("%lf",&p);
 81       printf("请输入存入存蓄年限:");
 82       scanf("%d",&year);
 83       printf("请输入年复利次数:");
 84       scanf("%d",&n);
 85        printf("请输入年终值:");
 86           scanf("%lf",&F);
 87       //F=p*pow((1+q/n),year*n);
 88           q=n*(pow(F/p,1.0/(year*n))-1);
 89       printf("复利的年利率为%lf:
",year,q);
 90 
 91   }
 92   if(I==5)
 93   {
 94       printf("1.按月投   2.按年投
");
 95           printf("请选择定投方式:");
 96           scanf("%d",&a);
 97           if(a==1){
 98               printf("请输入月投金额:");
 99               scanf("%lf",&p);
100               printf("请输入存入定投年限:");
101               scanf("%d",&year);
102               printf("请输入收益率:");
103               scanf("%lf",&q);
104             F=p*12*(1+q)*(-1+pow(1+q,year))/q;
105             printf("%d年后的资产终值为:%lf",year,F);}
106           if(a==2){
107               printf("请输入年投金额:");
108               scanf("%lf",&p);
109               printf("请输入存入定投年限:");
110               scanf("%d",&year);
111               printf("请输入收益率:");
112               scanf("%lf",&q);
113             F=p*(1+q)*(-1+pow(1+q,year))/q;
114             printf("%d年后的资产终值为:%lf",year,F);}
115 
116   }
117        
118   printf("
	是否要重新计算?(1/0)
");
119   scanf("%d",&ch);
120   if(ch==1)
121   {
122       goto B;
123   }
124 }
125 
126 //单利计算
127 void simpleinterest()
128 {
129     int n;
130     int ch;
131 A:printf("	1、获取单利终值
");
132   printf("	2、根据单利终值获取本金
");
133   printf("	3、估算获得年终值的年限
");
134   printf("	4、估算获得年终值的报酬率
");
135   printf("请选择功能<1~4>:");
136   scanf("%d",&n);
137   if(n==1)
138   {
139       
140       printf("请输入存款金额:");
141       scanf("%lf",&p);
142       printf("请输入存入存蓄年限:");
143       scanf("%d",&year);
144       printf("请输入年利率:");
145       scanf("%lf",&q);
146       
147       F=p*(1+q*year);
148       printf("单利终值为%lf
:",F);}
149   if(n==2)
150   {
151       printf("请输入单复利终值:");
152       scanf("%lf",&F);
153       printf("请输入存入存蓄年限:");
154       scanf("%d",&year);
155       printf("请输入年利率:");
156       scanf("%lf",&q);
157       
158       p=F/(1+q*year);
159       printf("本金为%lf
:",p);}
160   if(n==3){
161 
162           printf("请输入存款金额:");
163       scanf("%lf",&p);
164       printf("请输入年利率:");
165       scanf("%lf",&q);
166       
167       printf("请输入年终值:");
168           scanf("%lf",&F);
169 
170      year=(int)((F/p-1)/q);
171      // F=p*(1+q*year);
172       printf("年限为%d
:",year);
173 
174   }
175   if(n==4){
176       printf("请输入存款金额:");
177       scanf("%lf",&p);
178       printf("请输入存入存蓄年限:");
179       scanf("%d",&year);
180        printf("请输入年终值:");
181           scanf("%lf",&F);
182       //F=p*pow((1+q/n),year*n);
183           q=(F/p-1)/year;
184       printf("单利的年利率为%lf:
",q);
185 
186 
187   }
188   
189 
190   printf("
	是否要重新计算?(1/0)
");
191   scanf("%d",&ch);
192   if(ch==1)
193   {
194       goto A;
195 }}
196 
197 main(){
198     int ch,n;
199     while(1){
200         if(n==0) break;
201         innit();
202         menu();
203         printf("
");
204         printf("please chose<0~4>:");
205         scanf("%d",&ch);
206         switch(ch){
207         case 1:compoundinterest();break;
208         case 2:simpleinterest();break;
209         case 0:n=0;break;
210 }}}
211 //基金定投收益计算公式:
212 //M=a(1+x)[-1+(1+x)^n]/x
213 //M:预期收益
214 //a:每期定投金额
215 //x:收益率
216 //n:定投期数(公式中为n次方)
217 //注意a、x和n的匹配,月定投金额、月收益率、定投月数,如果是年,统一以后再计算。
218 //假设每月定投300元(每年为3600),年收益15%,定投35年。


8. 如果向银行贷款10万元,年利率6.5%,期限为10年,那么每月等额本息还款多少?(算复利条件下等额还款金额)
  1 #include<stdio.h>
  2 #include<math.h>
  3 int year,n;
  4 double p,q,F=0.0;
  5 void innit()
  6 {
  7     printf("注释:p为现金
");
  8     printf(" year为年限
");
  9     printf(" n为年复利次数
");
 10     printf(" q为年利率
");
 11     printf(" F为年复利终值
");
 12 }
 13 void menu()
 14 {
 15     printf("	|------------------------------------|
");
 16     printf("	|               welcome              |
");
 17     printf("	|------------------------------------|
");
 18     printf("	|          1、复利计算               |
");
 19     printf("	|          2、单利计算               |
");
 20     printf("	|          3、每月等额本息还款       |
");
 21     printf("	|          0、退出系统               |
");
 22     printf("	|------------------------------------|
");
 23 }
 24 //复利计算
 25 void compoundinterest(){
 26     int I,a;
 27     int ch;
 28 B:printf("	1、获取复利终值
");
 29   printf("	2、根据复利终值获取本金
");
 30   printf("	3、估算获得年终值的年限
");
 31   printf("	4、估算获得年终值的报酬率
");
 32   printf("	5、投资获利
");
 33   printf("请选择功能<1~5>:");
 34   scanf("%d",&I);
 35   if(I==1)
 36   {
 37       printf("请输入存款金额:");
 38       scanf("%lf",&p);
 39       printf("请输入存入存蓄年限:");
 40       scanf("%d",&year);
 41       printf("请输入年复利次数:");
 42       scanf("%d",&n);
 43       printf("请输入年利率:");
 44       scanf("%lf",&q);
 45       F=p*pow((1+q/n),year*n);
 46       printf("复利终值为%lf:",F);
 47   }
 48   if(I==2)
 49   {
 50       printf("请输入复利终值:");
 51       scanf("%lf",&F);
 52       printf("请输入存入存蓄年限:");
 53       scanf("%d",&year);
 54       printf("请输入年复利次数:");
 55       scanf("%d",&n);
 56       printf("请输入年利率:");
 57       scanf("%lf",&q);
 58       p=F/(pow((1+q/n),year*n));
 59       printf("复利本金为%lf:",p);
 60   }
 61   if(I==3)
 62   {
 63           printf("请输入存款金额:");
 64       scanf("%lf",&p);
 65       printf("请输入年利率:");
 66       scanf("%lf",&q);
 67       printf("请输入年复利次数:");
 68       scanf("%d",&n);
 69       printf("请输入年终值:");
 70           scanf("%lf",&F);
 71 
 72      year=(int)(log(F/p)/log(1+q/n))/n;
 73      // F=p*(1+q*year);
 74       printf("年限为%d
:",year);
 75 
 76 
 77   }
 78   if(I==4)
 79   {
 80       printf("请输入存款金额:");
 81       scanf("%lf",&p);
 82       printf("请输入存入存蓄年限:");
 83       scanf("%d",&year);
 84       printf("请输入年复利次数:");
 85       scanf("%d",&n);
 86        printf("请输入年终值:");
 87           scanf("%lf",&F);
 88       //F=p*pow((1+q/n),year*n);
 89           q=n*(pow(F/p,1.0/(year*n))-1);
 90       printf("复利的年利率为%lf:
",year,q);
 91 
 92   }
 93   if(I==5)
 94   {
 95       printf("1.按月投   2.按年投
");
 96           printf("请选择定投方式:");
 97           scanf("%d",&a);
 98           if(a==1){
 99               printf("请输入月投金额:");
100               scanf("%lf",&p);
101               printf("请输入存入定投年限:");
102               scanf("%d",&year);
103               printf("请输入收益率:");
104               scanf("%lf",&q);
105             F=p*12*(1+q)*(-1+pow(1+q,year))/q;
106             printf("%d年后的资产终值为:%lf",year,F);}
107           if(a==2){
108               printf("请输入年投金额:");
109               scanf("%lf",&p);
110               printf("请输入存入定投年限:");
111               scanf("%d",&year);
112               printf("请输入收益率:");
113               scanf("%lf",&q);
114             F=p*(1+q)*(-1+pow(1+q,year))/q;
115             printf("%d年后的资产终值为:%lf",year,F);}
116 
117   }
118        
119   printf("
	是否要重新计算?(1/0)
");
120   scanf("%d",&ch);
121   if(ch==1)
122   {
123       goto B;
124   }
125 }
126 
127 //单利计算
128 void simpleinterest()
129 {
130     int n;
131     int ch;
132 A:printf("	1、获取单利终值
");
133   printf("	2、根据单利终值获取本金
");
134   printf("	3、估算获得年终值的年限
");
135   printf("	4、估算获得年终值的报酬率
");
136   printf("请选择功能<1~4>:");
137   scanf("%d",&n);
138   if(n==1)
139   {
140       
141       printf("请输入存款金额:");
142       scanf("%lf",&p);
143       printf("请输入存入存蓄年限:");
144       scanf("%d",&year);
145       printf("请输入年利率:");
146       scanf("%lf",&q);
147       
148       F=p*(1+q*year);
149       printf("单利终值为%lf
:",F);}
150   if(n==2)
151   {
152       printf("请输入单复利终值:");
153       scanf("%lf",&F);
154       printf("请输入存入存蓄年限:");
155       scanf("%d",&year);
156       printf("请输入年利率:");
157       scanf("%lf",&q);
158       
159       p=F/(1+q*year);
160       printf("本金为%lf
:",p);}
161   if(n==3){
162 
163           printf("请输入存款金额:");
164       scanf("%lf",&p);
165       printf("请输入年利率:");
166       scanf("%lf",&q);
167       
168       printf("请输入年终值:");
169           scanf("%lf",&F);
170 
171      year=(int)((F/p-1)/q);
172      // F=p*(1+q*year);
173       printf("年限为%d
:",year);
174 
175   }
176   if(n==4){
177       printf("请输入存款金额:");
178       scanf("%lf",&p);
179       printf("请输入存入存蓄年限:");
180       scanf("%d",&year);
181        printf("请输入年终值:");
182           scanf("%lf",&F);
183       //F=p*pow((1+q/n),year*n);
184           q=(F/p-1)/year;
185       printf("单利的年利率为%lf:
",q);
186 
187 
188   }  
189 
190   printf("
	是否要重新计算?(1/0)
");
191   scanf("%d",&ch);
192   if(ch==1)
193   {
194       goto A;
195 }}
196 
197 
198 void DengEHuanKuan(){
199      printf("请输入贷款金额:");
200               scanf("%lf",&F);
201               printf("请输入存入贷款年限:");
202               scanf("%d",&year);
203               printf("请输入年利率:");
204               scanf("%lf",&q);
205             p=F*q/(12*(1+q)*(-1+pow(1+q,year)));
206             printf("每月等额本息还款为:%lf",p);
207 }
208 
209 
210 
211 main(){
212     int ch,n;
213     while(1){
214         if(n==0) break;
215         innit();
216         menu();
217         printf("
");
218         printf("please chose<0~4>:");
219         scanf("%d",&ch);
220         switch(ch){
221         case 1:compoundinterest();break;
222         case 2:simpleinterest();break;
223         case 3:DengEHuanKuan();break;
224         case 0:n=0;break;
225 }}}
226 //基金定投收益计算公式:
227 //M=a(1+x)[-1+(1+x)^n]/x
228 //M:预期收益
229 //a:每期定投金额
230 //x:收益率
231 //n:定投期数(公式中为n次方)
232 //注意a、x和n的匹配,月定投金额、月收益率、定投月数,如果是年,统一以后再计算。
233 //假设每月定投300元(每年为3600),年收益15%,定投35年。


 
原文地址:https://www.cnblogs.com/4249ken/p/5282805.html