复利计算
#include<stdio.h> #include<math.h> double principal=0,mount=0; int year=0; int n=1; double rate; void danli(){ printf("请输入的本金:"); scanf("%lf",&principal); printf("请输入年限:"); scanf("%d",&year);
printf("请输入年利率:"); scanf("%lf",&rate); mount=principal+principal*rate*year; printf("本息为:%.2lf\n",mount); } void fuli(){ printf("请输入本金:"); scanf("%lf",&principal); printf("请输入年限:"); scanf("%d",&year); printf("请输入年利率:"); scanf("%lf",&rate); printf("请输入年复利次数:"); scanf("%d",&n); rate=pow((1+rate/n),n)-1; mount=principal*pow((1+rate),year); printf("本息为:%.2lf\n",mount); } void principaljisuan(){ printf("请输入期望的收益:"); scanf("%lf",&mount); printf("请输入年限:"); scanf("%d",&year); printf("请输入年利率:"); scanf("%lf",&rate); printf("请输入年复利次数:"); scanf("%d",&n); rate=pow((1+rate/n),n)-1; principal=mount/pow((1+rate),year); printf("输出所需本金为:%.2lf\n",principal); } main(){ int num=0; printf("|----------------------------------------------------------|\n"); printf("| 1.复利计算 |\n"); printf("| 2.单利计算 |\n"); printf("| 3.计算本金 |\n"); printf("|----------------------------------------------------------|\n"); printf("请输入选择:"); scanf("%d",&num); switch(num){ case 1: fuli(); break; case 2: danli(); break; case 3: principaljisuan(); default: return 0; } }