天天看點

0319複利計算05

6.如果每年都将積蓄的3萬元進行投資,每年都能獲得3%的回報,然後将這些本利之和連同年金再投入新一輪的投資,那麼,30年後資産總值将變為多少?如果換成每月定投3000呢?

(定額定投收益計算辦法)

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\n",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年。      

運作結果:

0319複利計算05
0319複利計算05
0319複利計算05