C Simple example Example of finding the time it takes to double the principal with compound interest. While the door is weird.

Asked 2 years ago, Updated 2 years ago, 121 views

int years = 0;
double rate, investment;

printf ("Principal :");
scanf("%f", &investment);
printf ("Rate :");
scanf("%f", &rate);

double total = investment;

while (total < investment * 2)
{
    total = total * (1 + rate);
    years += 1;
}
printf("%d years";

return 0;

Why can't I print it out because the years repetition is applied within the while statement?

c scanf

2022-09-20 13:32

1 Answers

The format character for reading the float variable in scanf is "%f", but you must use "%lf" to read the double variable.


2022-09-20 13:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.