SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Recursion
Exercise:
Programming Projects
Question:8 | ISBN:9780132846813 | Edition: 5

Question

A savings account typically accrues savings using compound interest. If you deposit $1000 with a 10% interest rate per year, after one year you will have $1100. If you leave this money in the account for another year at 10% interest, you will have $1210. After three years you will have $1331, and so on.

Write a program that inputs the initial amount, an interest rate per year, and the number of years the money will accrue compound interest. Write a recursive function that calculates the amount of money that will be in the savings account using the input information.

To verify your function, the amount should be equal to P(1+i)n, where P is the amount initially saved, i is the interest rate per year, and n is the number of years.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
#include <cmath>
using namespace std;
// Recursive function to calculate the amount with compound interest
double calculateCompoundInterest(double initial_amount, double interest_rate, int num_years)
{
    // Base case: if no years left, return the initial amount
    if (num_years == 0)
    {
        return initial_amount;
    }

    // Recursive case: calculate the amount after one year with compound interest
    double amount_after_one_year = initial_amount * (1 + interest_rate);
    
    // Recur for (num_years - 1) to calculate the amount after the remaining years
    return calculateCompoundInterest(amount_after_one_year, interest_rate, num_years - 1);
}

int main()
{
    //Declare variables
    double initial_amount, interest_rate;
    int num_years;

    // Input the initial amount, interest rate, and number of years
    cout << "Enter the initial amount: $";
    cin >> initial_amount;
    cout << "Enter the interest rate per year (in decimal): ";
    cin >> interest_rate;
    cout << "Enter the number of years: ";
    cin >> num_years;

    // Calculate the amount using the recursive function
    double amount = calculateCompoundInterest(initial_amount, interest_rate, num_years);

    // Output the result
    cout << "The amount after " << num_years << " years is: $" << amount << endl;

    return 0;
}

OUTPUT:

Enter the initial amount: $50000
Enter the interest rate per year (in decimal): 2
Enter the number of years: 10
The amount after 10 years is: $2.95245e+09

 

0 0

Discussions

Post the discussion to improve the above solution.