08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5 ■ Programs with Repetition Logic

A solution to this problem is given in Program P5.18.

Program P5.18

#include <stdio.h>

int main() {

int year;

double initialDeposit, interestRate, target, deposit, interest;

printf("Initial deposit? ");

scanf("%lf", &initialDeposit);

printf("Rate of interest? ");

scanf("%lf", &interestRate);

printf("Target deposit? ");

scanf("%lf", &target);

}

printf("\nYear Deposit Interest\n\n");

deposit = initialDeposit;

year = 0;

do {

year++;

interest = deposit * interestRate / 100;

printf("%3d %8.2f %8.2f\n", year, deposit, interest);

deposit += interest;

} while (deposit <= target);

printf("\nDeposit exceeds $%7.2f at the end of year %d\n", target, year);

The program uses the following variables:

initialDeposit

interestRate

target

deposit

1000, in the example

10, in the example

2000, in the example

the deposit at any given time

135

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!