0% found this document useful (0 votes)
67 views

Practical - Tutorial 4 - 3

This document contains a series of programming exercises involving loops, conditionals, arrays, and other basic programming concepts. The exercises include: 1) Finding errors in code snippets 2) Writing code to print sequences and perform calculations like summing values 3) Using formulas to calculate amounts in savings accounts with different interest rates 4) Analyzing customer credit limits after a company cuts them in half 5) Calculating total retail value from products sold using a switch statement
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Practical - Tutorial 4 - 3

This document contains a series of programming exercises involving loops, conditionals, arrays, and other basic programming concepts. The exercises include: 1) Finding errors in code snippets 2) Writing code to print sequences and perform calculations like summing values 3) Using formulas to calculate amounts in savings accounts with different interest rates 4) Analyzing customer credit limits after a company cuts them in half 5) Calculating total retail value from products sold using a switch statement
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL / TUTORIAL 4_3

1. Find the error in each of the following. (Note: There may be more than one error.)

a) for ( x = 100, x >= 1, x++ )


printf( "%d\n", x );

b) The following code should print whether a given integer is odd or even:

switch ( value % 2 ) {
case 0:
printf( "Even integer\n" );
case 1:
printf( "Odd integer\n" );
}

c) The following code should input an integer and a character and print them. Assume
the user types as input 100 A.

scanf( "%d", &intVal );


charVal = getchar();
printf( "Integer: %d\nCharacter: %c\n", intVal, charVal );

d) for ( x = .000001; x == .0001; x += .000001 ) {


printf( "%.7f\n", x );
}

e) The following code should output the odd integers from 999 to 1:
for ( x = 999; x >= 1; x += 2 ) {
printf( "%d\n", x );
}

f) The following code should output the even integers from 2 to 100:
counter = 2;
do {
if ( counter % 2 == 0 ) {
printf( "%d\n", counter );
}
counter += 2;

1
} While ( counter < 100 );

g) The following code should sum the integers from 100 to 150 (assume total is
initialized to 0):
for ( x = 100; x <= 150; x++ ); {
total += x;
}

2. State which values of the control variable x are printed by each of the following for
statements:

a) for ( x = 2; x <= 13; x += 2 ) {


printf( "%d\n", x );
}

b) for ( x = 5; x <= 22; x += 7 ) {


printf( "%d\n", x );
}

c) for ( x = 3; x <= 15; x += 3 ) {


printf( "%d\n", x );
}

d) for ( x = 1; x <= 5; x += 7 ) {
printf( "%d\n", x );
}

e) for ( x = 12; x >= 2; x -= 3 ) {


printf( "%d\n", x );
}

3 Write for statements that print the following sequences of values:

a) 1, 2, 3, 4, 5, 6, 7
b) 3, 8, 13, 18, 23
c) 20, 14, 8, 2, –4, –10
d) 19, 27, 35, 43, 51

2
4. What does the following program do?

5. Write a program that sums a sequence of integers. Assume that the first integer read
with scanf specifies the number of values remaining to be entered. Your program
should read only one value each time scanf is executed. A typical input sequence
might be

5 100 200 300 400 500

where the 5 indicates that the subsequent five values are to be summed.

3
6. Write a program that calculates and prints the average of several integers. Assume
the last value read with scanf is the sentinel 9999. A typical input sequence might
be
10 8 11 7 9 9999
indicating that the average of all the values preceding 9999 is to be calculated.

7. Write a program that finds the smallest of several integers. Assume that the first
value read specifies the number of values remaining.

8. Write a program that calculates and prints the sum of the even integers from 2 to 30.

9. Write a program that calculates and prints the product of the odd integers from 1 to
15.

10. The factorial function is used frequently in probability problems. The factorial of a
positive integer n is equal to the product of the positive integers from 1 to n. Write a
program that evaluates the factorials of the integers from 1 to 5. Print the results in
tabular format. What difficulty might prevent you from calculating the factorial of
20?

11. A person invests $1000.00 in a savings account yielding 5% interest. Assuming that
all interest is left on deposit in the account, calculate and print the amount of money
in the account at the end of each year for 10 years. Use the following formula for
determining these amounts:

where
p is the original amount invested (i.e., the principal)
r is the annual interest rate
n is the number of years
a is the amount on deposit at the end of the nth year.

Modify the above program to repeat its steps for interest rates of 5%, 6%, 7%, 8%,
9%, and 10%. Use a for loop to vary the interest rate.

12. Collecting money becomes increasingly difficult during periods of recession, so


companies may tighten their credit limits to prevent their accounts receivable
(money owed to them) from becoming too large. In response to a prolonged
recession, one company has cut its customers’ credit limits in half. Thus, if a
particular customer had a credit limit of $2000, it’s now $1000. If a customer had a
credit limit of $5000, it’s now $2500. Write a program that analyzes the credit status
of three customers of this company. For each customer you’re given:

4
a) The customer’s account number
b) The customer’s credit limit before the recession
c) The customer’s current balance (i.e., the amount the customer owes the company).

Your program should calculate and print the new credit limit for each customer and
should determine (and print) which customers have current balances that exceed
their new credit limits.

13. An online retailer sells five different products whose retail prices are shown in the
following table:

Write a program that reads a series of pairs of numbers as follows:

a) Product number
b) Quantity sold for one day

Your program should use a switch statement to help determine the retail price for
each product.
Your program should calculate and display the total retail value of all products sold
last week.

You might also like