Program C HW
Program C HW
4.5
Find the errors of the following. (Note: There may be more than one error.)
a) For ( x = 100, x >= 1, x++ )
printf( %d\n, x );
ans:
for (x = 100; x >= 1; x--)
printf ("%d\n", x);
b) The following code should print wether a given integer is odd or even:
ans:
switch ( value % 2 ) {
case 0:
printf( Even integer\n );
case 1:
printf( Odd integer\n );
}
switch ( value % 2 ) {
case 0:
printf( Even integer\n );
break:
case 1:
printf( Odd integer\n );
break;
}
c) The following code should input an integer and a character and print them. Assume
the user type as input 100 A.
scanf( %d, &intval );
charval = gerchar();
printf( integer: %d\nCharacter: %c\n, intval, charval);
d)
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;
}while ( counter < 100 );
g) The following code should sum th4e integers from 100 to 150 (assume total is
initialized to 0);
for ( x = 100; x <= 150; x++ ); {
total += x;
}
4.6
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)
c)
d)
for ( x = 1; x <= 5; x += 7 ) {
printf( %d\n, x );
}
e)
4.7
4,8
4.9
4.10
(Average a Sequence of Integers) Write a program that calculates and prints the
average of several integers. Assume the last value read with with scanf is the sentinel 9999. A typical input sequence might be
10 8 11 7 9 9999
(Find the Smallest) Write a program that finds the smallest of several integers.
Assume that the first value read specific the number of values remaining.
4.12
(Calculating the Sum of Even Integers) Write a program that calculate and prints
the sum of the even integers form 2 to 30.
4.13
(Calculating the Product of Odd Integers) Write a program that calculates and prints the product of the odd integers from 1 to 15.
(Factorials) The factorial function is used frequently in probability problems. The factorial of a positive integers n (written n! and pronounced n factorial) is
equal to the product of the positive integers from 1 to n. Write a program that
evaluate the factorial of the integers from 1 to 5. Print the result in tabular format. What difficulty might prevent you from calculating the factorial of 20?
4.14
4.15
4.16
(Triangle Printing Program) Write a program that prints the following patterns
separately, one below the other. Use for loop to generate the patterns. All asterisks (*) should be printed by a single printf statement of the form printf( * );
(this causes the asterisks to print side by side).
[Hint: The last two pattrens require that each line begin with an appropriate
number of blanks.]
(A)
*
**
***
****
*****
******
*******
********
*********
**********
4.17
(B)
**********
*********
********
*******
******
*****
****
***
**
*
(C)
**********
*********
********
*******
******
*****
****
***
**
*
(D)
*
**
***
****
*****
******
*******
********
*********
**********
(Calculating Credit Limits) Collecting money becomes increasingly difficult during periods of recession, so companies may tighten their credit limits to prevents
their accounts receivable (money owed to them) from becoming too larg. 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, its now $1000. If
a customer had a credit limit of $5000, its now $2500. Write a program that
analyzes the credit statues of three customers of this company. For each customer youre given:
a)
The Customers account number
b)
The customers credit limits before the recession
c)