Practical - Tutorial 4 - 3
Practical - Tutorial 4 - 3
1. Find the error in each of the following. (Note: There may be more than one error.)
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.
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:
d) for ( x = 1; x <= 5; x += 7 ) {
printf( "%d\n", x );
}
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
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.
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:
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.