roredd
roredd

Reputation: 13

Calculating time for any sum to double, at a given rate of interest compounded annually

I was trying to write a program that calculates the time it takes for any sum of money to double at a given rate of interest compounded annually.

When I run this program, I have found that

What am I doing wrong?

int main(){
    cout << "Please enter the interest rate in % per annum:";
    int counter = 0;
    int sum=100;
    int interest = 0;
    cin >> interest;
    while(sum<200){
        counter++;
        sum += sum*(interest / 100);
        }
    cout << "\n It would take about " << counter << " years to double";
  }

Upvotes: 1

Views: 121

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117926

interest is an int so this line

interest / 100

is doing integer division, and will always be 0. The quick fix would be to change the literal so you are doing floating point math

sum += sum*(interest / 100.0);

Upvotes: 1

Related Questions