C++ program to calculate how many years needed to get X rupees with 1% interest


Suppose we have a number X. We have 100 rupees in a bank. The bank returns annual interest rate of 1 % compounded annually. (Integers only). We have to check how many years we need to get X rupees?

So, if the input is like X = 520, then the output will be 213.

Steps

To solve this, we will follow these steps −

sum := 0
balance := 100
while balance < n, do:
   interest := balance / 100
   sum := sum + 1
   balance := balance + interest
return sum

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

int solve(int n){
   int sum = 0;
   int balance = 100;
   while (balance < n){
      int interest = balance / 100;
      sum = sum + 1;
      balance = balance + interest;
   }
   return sum;
}
int main(){
   int X = 520;
   cout << solve(X) << endl;
}

Input

520

Output

213

Updated on: 03-Mar-2022

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements