C语言例程:投资问题 单利和复利

投资问题:A以每年的10%的单利息投资100美元,B以每年5%的复合利息投资了100美元。编写一个程序,计算需要多少年B的投资总额才会超过A的,并且显示出AB的资产总额。
p为本金 r为单利 n为年 总和=p*(1+rn)
p为本金 r为复利 n为年 总和=p
((1+r)n次方)

#include <stdio.h>
#include <stdlib.h>
#define INIT	100
#define SIMP	0.10
#define MULT	0.05

static void test(void)
{	
    int year = 0;
    double suma = INIT,sumb = INIT;
    
    while(sumb <= suma)
    {
        suma += INIT * SIMP;
        sumb += MULT * sumb;
        year ++;
    }
    printf("year = %d\n",year);
    printf("suma = %f\tsumb = %f\n",suma,sumb);
}

int main()
{
    test();
    exit(0);
}

猜你喜欢

转载自blog.csdn.net/weixin_45075787/article/details/114222796
今日推荐