# Robson » Little Red Book » Interest on a deposit

## Chapter 3 - Question 10

Design a program to compute the balance of a deposit account at the end of a given period when the interest rate and initial balance are known.

### Solution 1

Example use:

`C:\code>Interest 3Starting balance is 188. Interest rate is 4%Balance after 1 month(s) is 195.52Balance after 2 month(s) is 203.341Balance after 3 month(s) is 211.474Final balance is 211.474C:\code>`
`#include <time.h>`
`#include <math.h>`
`#include <iostream.h>`
` `
`using namespace std;`
` `
`int main(int argc, char *argv[])`
`{`
`    // make sure random numbers are random`
`    srand(time(NULL));`
`    rand();`
` `
`    // the amount of months is given by the user`
`    int iMonths = atoi(argv[1]);    `
`    // generate a random interest rate`
`    // between 0.5 and 5 in 0.5 increments`
`    float fInterest = (10 * rand()) / RAND_MAX;`
`    fInterest /= 2;`
`    // generate a random starting balance`
`    // between 1 and 2000`
`    float fBalance = ((2000 * rand()) / RAND_MAX)+1;`
`    `
`    // i've already used iostream.h, but i prefer printf`
`    cout << "Starting balance is " << fBalance << " Interest rate is " << fInterest << "%\n";`
`    // now loop through each month and add interest to the total`
`    for (int n = 1; n <= iMonths; n++)`
`    {`
`        // add up the interest for this month`
`        fBalance += fBalance * (fInterest / 100);`
`        // show the balanace after this many months`
`        cout << "Balance after " << n << " month(s) is " << fBalance << "\n";`
`    }`
`    // show the final balance`
`    cout << "Final balance is " << fBalance;`
`}`
` `

## Log

• May 2, 2005 - Added solution 1.
© 2004-19 robson | cc unless stated