0% found this document useful (0 votes)
295 views4 pages

Program C HW

The document contains descriptions of programming exercises involving loops, conditionals, arrays, functions, and other programming concepts. It includes exercises to: - Find errors in code snippets using loops and conditionals - Write code to print sequences, calculate sums and averages, find minimums/maximums - Write programs to calculate factorials, print patterns, calculate credit limits, and print bar charts - Explain what programs with nested loops and conditionals are doing The exercises cover basic programming concepts like loops, conditionals, functions, arrays, input/output, and mathematical/statistical operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
295 views4 pages

Program C HW

The document contains descriptions of programming exercises involving loops, conditionals, arrays, functions, and other programming concepts. It includes exercises to: - Find errors in code snippets using loops and conditionals - Write code to print sequences, calculate sums and averages, find minimums/maximums - Write programs to calculate factorials, print patterns, calculate credit limits, and print bar charts - Explain what programs with nested loops and conditionals are doing The exercises cover basic programming concepts like loops, conditionals, functions, arrays, input/output, and mathematical/statistical operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Exercises

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)

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


printf(%.7f\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;
}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)

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 );
}

4.7

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

4,8

What does the following program do?


#include<stdio.h>
int main (void)
{
int x;
int y;
int i;
int j;
printf( Enter two integer in the range 1-20: );
scanf( %d%d, &x, &y );
for ( i = 1; I <= y: i++ ) {
for ( j =1; i <= x; j++ ) {
printf( @ );
}
printf( \n );
}
return 0;
}

4.9

(Sum a Sequence of integers) Write a program that sums a sequence on integer.


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 4 indicates that the subsequent five value are to be summed.

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

indicating the average of all the values preceding 9999 is to be calculated.


4.11

(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

(Modified Compound interest program) Modify the compound-interest program


of Section 4.6 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.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)

The customers 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.
4.18

(Bar Chat Printing Program) One interesting application of computers is drawing


graphs and bar charts (sometimes called histograms). Write a program that
reads five numbers (each between 1 and 30). For each number read , your program should print a line containing that number of adjacent asterisks. For example, if your program readas the number seven, it should print *******.

You might also like