TCS MockVita 2 Solution : Problem B

Problem : Saving for a rainy day

By nature, an average Indian believes in saving money. Some reports suggest that an average Indian manages to save approximately 30+% of his salary. Dhaniram is one such hard working fellow. With a view of future expenses, Dhaniram resolves to save a certain amount in order to meet his cash flow demands in the future.



Consider the following example.
Dhaniram wants to buy a TV. He needs to pay Rs 2000/per month for 12 installments to own the TV. If let's say he gets 4% interest per annum on his savings bank account, then Dhaniram will need to deposit a certain amount in the bank today, such that he is able to withdraw Rs 2000/per month for the next 12 months without requiring any additional deposits throughout.

Your task is to find out how much Dhaniram should deposit today so that he gets assured cash flows for a fixed period in the future, given the rate of interest at which his money will grow during this period.

Input Format:
First line contains desired cash flow M
Second line contains period in months denoted by T
Third line contains rate per annum R expressed in percentage at which deposited amount will grow

Output Format:
Print total amount of money to be deposited now rounded off to the nearest integer

Constraints:
M > 0
T > 0
R >= 0
Calculation should be done upto 11digit precision

Sample Input 1

500
3
12

Sample Output 1

1470

Sample Input 2

6000
3
5.9

Sample Output 2

17824

Sample Input 3

500
2
0

Sample Output 3

1000

C++ Implementation:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
 int emi,months,i=0;
 long double rate;
 long double assumed_output;
 long double interest;
 long double real_output;
 cin>>emi>>months>>rate;
 rate = rate / 12; //converting rate per annum to rate per month
 assumed_output = emi * months;
 //to find the difference bw real output and assumed output
 
 /* for every month calculate the interest on current amount and subtract the emi */
 while(i<months)
 {
  interest = assumed_output + (assumed_output * (rate/100));
  assumed_output = interest - emi;
  i++;
 }
 real_output = (emi * months) - assumed_output;
 cout<<ceil(real_output);
 
 return 0;
}
Note: Second test case is failing, for which solutions can be contributed to coderegister.co.in , by pasting the code in comments section below or mailing to "sathish@coderegister.co.in"

Other Problems from TCS Mockvita - 1 and Mockvita - 2:

Solution : MockVita-I Problem D

Solution : MockVita-II Problem C

Solution : MockVita-II Problem B

Solution : MockVita-II Problem D

Solution : MockVita-II Problem A

Solution : MockVita-I Problem B

Solution : MockVita-I Problem C

Solution : MockVita-I Problem F

Solution : MockVita-I Problem A