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 3 ■ Programs with Sequence Logic

Program P3.6

//calculate interest and service charge for bank customer

#include <stdio.h>

int main() {

char customer[30], acctNum[30];

double avgBalance, interest, service;

int numTrans;

printf("Name? ");

gets(customer);

printf("Account number? ");

gets(acctNum);

printf("Average balance? ");

scanf("%lf", &avgBalance);

printf("Number of transactions? ");

scanf("%d", &numTrans);

interest = avgBalance * 0.06;

service = numTrans * 0.50;

printf("\nName: %s\n", customer);

printf("Average balance: $%3.2f\n", avgBalance);

printf("Interest: $%3.2f\n", interest);

printf("Service charge: $%3.2f\n", service);

}

This problem is more complicated than those we have seen so far. It involves more data and

more processing. But we can simplify its solution if we tackle it in small steps.

Firstly, let us outline an algorithm for solving the problem. This can be:

prompt for and read each item of data

calculate interest earned

calculate service charge

print required output

The logic here is fairly straightforward and a little thought should convince us that these are

the steps required to solve the problem.

Next, we must choose variables for the data items we need to store.

58

• For the customer’s name, we need a string variable—we call it customer.

• We may be tempted to use an integer variable for the account number but

this is not a good idea for two reasons: an account number may contain

letters (as in CD55887700); or it may be a very long integer, too big to fit in an

int variable. For these reasons, we use a string variable that we call acctNum.

• The average balance may contain a decimal point and must be stored in a

double variable; we call it avgBalance.

• The number of transactions is a whole number so we use an

int variable, numTrans.

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!