天天看點

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為現金\n");
  8     printf(" year為年限\n");
  9     printf(" n為年複利次數\n");
 10     printf(" q為年利率\n");
 11     printf(" F為年複利終值\n");
 12 }
 13 void menu()
 14 {
 15     printf("\t|------------------------------------|\n");
 16     printf("\t|               welcome              |\n");
 17     printf("\t|------------------------------------|\n");
 18     printf("\t|          1、複利計算               |\n");
 19     printf("\t|          2、單利計算               |\n");
 20     printf("\t|          0、退出系統               |\n");
 21     printf("\t|------------------------------------|\n");
 22 }
 23 //複利計算
 24 void compoundinterest(){
 25     int I;
 26     int ch;
 27 B:printf("\t1、擷取複利終值\n");
 28   printf("\t2、根據複利終值擷取本金\n");
 29   printf("\t3、估算獲得年終值的年限\n");
 30   printf("\t4、估算獲得年終值的報酬率\n");
 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\n:",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:\n",q);
 89 
 90   }
 91   printf("\n\t是否要重新計算?(1/0)\n");
 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("\t1、擷取單利終值\n");
105   printf("\t2、根據單利終值擷取本金\n");
106   printf("\t3、估算獲得年終值的年限\n");
107   printf("\t4、估算獲得年終值的報酬率\n");
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\n:",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\n:",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\n:",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:\n",q);
158 
159 
160   }
161   printf("\n\t是否要重新計算?(1/0)\n");
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/複利計算——算年限和年利率      
0316複利和單利計算更新4
0316複利和單利計算更新4
2、單利計算年限和年利率      
0316複利和單利計算更新4
0316複利和單利計算更新4

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為現金\n");
  8     printf(" year為年限\n");
  9     printf(" n為年複利次數\n");
 10     printf(" q為年利率\n");
 11     printf(" F為年複利終值\n");
 12 }
 13 void menu()
 14 {
 15     printf("\t|------------------------------------|\n");
 16     printf("\t|               welcome              |\n");
 17     printf("\t|------------------------------------|\n");
 18     printf("\t|          1、複利計算               |\n");
 19     printf("\t|          2、單利計算               |\n");
 20     printf("\t|          0、退出系統               |\n");
 21     printf("\t|------------------------------------|\n");
 22 }
 23 //複利計算
 24 void compoundinterest(){
 25     int I,a;
 26     int ch;
 27 B:printf("\t1、擷取複利終值\n");
 28   printf("\t2、根據複利終值擷取本金\n");
 29   printf("\t3、估算獲得年終值的年限\n");
 30   printf("\t4、估算獲得年終值的報酬率\n");
 31   printf("\t5、投資獲利\n");
 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\n:",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:\n",year,q);
 90 
 91   }
 92   if(I==5)
 93   {
 94       printf("1.按月投   2.按年投\n");
 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("\n\t是否要重新計算?(1/0)\n");
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("\t1、擷取單利終值\n");
132   printf("\t2、根據單利終值擷取本金\n");
133   printf("\t3、估算獲得年終值的年限\n");
134   printf("\t4、估算獲得年終值的報酬率\n");
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\n:",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\n:",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\n:",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:\n",q);
185 
186 
187   }
188   
189 
190   printf("\n\t是否要重新計算?(1/0)\n");
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("\n");
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為現金\n");
  8     printf(" year為年限\n");
  9     printf(" n為年複利次數\n");
 10     printf(" q為年利率\n");
 11     printf(" F為年複利終值\n");
 12 }
 13 void menu()
 14 {
 15     printf("\t|------------------------------------|\n");
 16     printf("\t|               welcome              |\n");
 17     printf("\t|------------------------------------|\n");
 18     printf("\t|          1、複利計算               |\n");
 19     printf("\t|          2、單利計算               |\n");
 20     printf("\t|          3、每月等額本息還款       |\n");
 21     printf("\t|          0、退出系統               |\n");
 22     printf("\t|------------------------------------|\n");
 23 }
 24 //複利計算
 25 void compoundinterest(){
 26     int I,a;
 27     int ch;
 28 B:printf("\t1、擷取複利終值\n");
 29   printf("\t2、根據複利終值擷取本金\n");
 30   printf("\t3、估算獲得年終值的年限\n");
 31   printf("\t4、估算獲得年終值的報酬率\n");
 32   printf("\t5、投資獲利\n");
 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\n:",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:\n",year,q);
 91 
 92   }
 93   if(I==5)
 94   {
 95       printf("1.按月投   2.按年投\n");
 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("\n\t是否要重新計算?(1/0)\n");
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("\t1、擷取單利終值\n");
133   printf("\t2、根據單利終值擷取本金\n");
134   printf("\t3、估算獲得年終值的年限\n");
135   printf("\t4、估算獲得年終值的報酬率\n");
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\n:",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\n:",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\n:",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:\n",q);
186 
187 
188   }  
189 
190   printf("\n\t是否要重新計算?(1/0)\n");
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("\n");
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年。