4 C P rogram Control Sample solved questions from the text book . Solve them before Exam -1. These questions and answers are supported by author and not by the instructor: Prof. Yahia Halabi - PSUT. I select ed solutions to practice my student to help them, in addit ion to my lectures . Thanks too, to Pearson Education Inc. publishing company. Approved
26
Embed
Sample of Questions and Answers- Text Book Material Doc
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
4C Program Control
Sample solved questionsfrom the text book.
Solve them before Exam-1.
These questions and answers aresupported by author and not by theinstructor: Prof. Yahia Halabi - PSUT.
I selected solutions to practice mystudent to help them, in addition to mylectures.Thanks too, to Pearson Education Inc.
Self-Review Exercises4.1 Fill in the blanks in each of the following statements.
a) Counter-controlled repetition is also known as repetition because it isknown in advance how many times the loop will be executed.
ANS: definite.b) Sentinel-controlled repetition is also known as repetition because it is not
known in advance how many times the loop will be executed.ANS: indefinite.c) In counter-controlled repetition, a(n) is used to count the number of times a
group of instructions should be repeated.ANS: control variable or counter.d) The statement, when executed in a repetition statement, causes the next
iteration of the loop to be performed immediately.ANS: continue.e) The statement, when executed in a repetition statement or a switch, causes
an immediate exit from the statement.ANS: break.f) The is used to test a particular variable or expression for each of the con-
stant integral values it may assume.ANS: switch selection statement.
4.2 State whether the following are true or false. If the answer is false, explain why.a) The default case is required in the switch selection statement.ANS: False. The default case is optional. If no default action is needed, then there is no
need for a default case.b) The break statement is required in the default case of a switch selection statement.ANS: False. The break statement is used to exit the switch statement. The break statement
is not required when the default case is the last case.c) The expression (x > y && a < b) is true if either x > y is true or a < b is true.ANS: False. Both of the relational expressions must be true in order for the entire expression
to be true when using the && operator.d) An expression containing the || operator is true if either or both of its operands is true.ANS: True.
4.3 Write a statement or a set of statements to accomplish each of the following tasks:a) Sum the odd integers between 1 and 99 using a for statement. Assume the integer vari-
ables sum and count have been defined.ANS: sum = 0;
for ( count = 1; count <= 99; count += 2 )
sum += count;
b) Print the value 333.546372 in a field width of 15 characters with precisions of 1, 2, 3, 4and 5. Left justify the output. What are the five values that print?
ANS: printf( "%-15.1f\n", 333.546372 ); /* prints 333.5 */
printf( "%-15.2f\n", 333.546372 ); /* prints 333.55 */
printf( "%-15.3f\n", 333.546372 ); /* prints 333.546 */
printf( "%-15.4f\n", 333.546372 ); /* prints 333.5464 */
printf( "%-15.5f\n", 333.546372 ); /* prints 333.54637 */
c) Calculate the value of 2.5 raised to the power of 3 using the pow function. Print the re-sult with a precision of 2 in a field width of 10 positions. What is the value that prints?
d) Print the integers from 1 to 20 using a while loop and the counter variable x. Assumethat the variable x has been defined, but not initialized. Print only five integers per line.[Hint: Use the calculation x % 5. When the value of this is 0, print a newline character,otherwise print a tab character.]
ANS: x = 1;while ( x <= 20 ) {
printf( "%d", x );
if ( x % 5 == 0 )
printf( "\n" );
else
printf( "\t" );
x++;
}
orx = 1;
while ( x <= 20 )
if ( x % 5 == 0 )
printf( "%d\n", x++ );
else
printf( "%d\t", x++ );
or
x = 0;
while ( ++x <= 20 )
if ( x % 5 == 0 )
printf( "%d\n", x );
else
printf( "%d\t", x );
e) Repeat Exercise 4.3 (d) using a for statement.ANS: for ( x = 1; x <= 20; x++ ) {
printf( "%d", x );
if ( x % 5 == 0 )
printf( "\n" );
else
printf( "\t" );
}
or
for ( x = 1; x <= 20; x++ )
if ( x % 5 == 0 )
printf( "%d\n", x );
else
printf( "%d\t", x );
4.4 Find the error in each of the following code segments and explain how to correct it.
ANS: Error: The semicolon after the while header causes an infinite loop.Correction: Replace the semicolon with a { or remove both the ; and the }.
b) for ( y = .1; y != 1.0; y += .1 )
printf( "%f\n", y );
ANS: Error: Using a floating-point number to control a for repetition statement.Correction: Use an integer, and perform the proper calculation in order to get thevalues you desire.
for ( y = 1; y != 10; y++ )printf( "%f\n", ( float ) y / 10 );
c) switch ( n ) {
case 1:printf( "The number is 1\n" );
case 2:printf( "The number is 2\n" );
break;
default:
printf( "The number is not 1 or 2\n" );
break;
}
ANS: Error: Missing break statement in the statements for the first case.Correction: Add a break statement at the end of the statements for the first case.Note that this is not necessarily an error if you want the statement of case 2: to exe-cute every time the case 1: statement executes.
d) The following code should print the values 1 to 10.
n = 1;while ( n < 10 )printf( "%d ", n++ );
ANS: Error: Improper relational operator used in the while repetition-continuation condi-tion.Correction: Use <= rather than <.
Exercises4.5 Find the error in each of the following (Note: there maybe more than one error):
a) For ( x = 100, x >= 1, x++ )
printf( "%d\n", x );
ANS: F in for should be lowercase. The infinite loop can be corrected by switching the 1
and the 100 and changing the relational operator to <=. Semicolons are needed be-tween the for conditions, not comma operators.for ( x = 1; x <= 100; x++ )
printf( “%d\n”, x);
b) The following code should print whether a given integer is odd or even:
ANS: charVal will read the blank character when the user types in intVal and hits return.To correct this, scanf should be used to read in charVal.scanf( “%d”, &intVal );
printf( "%.7f\n", x );ANS: In addition, floating-point numbers should never be compared with == or != due to
imprecision. This imprecision often causes infinite loops to occur. To correct this, aninteger variable should be used in the for loop.
e) The following code should output the odd integers from 999 to 1:
for ( x = 999; x >= 1; x += 2 )printf( "%d\n", x );
ANS: loop should be decrementing not incrementing.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;} While ( counter < 100 );
ANS: D in Do should be lowercase. W in While should be lowercase. The range of 2 to 100
needs to be printed, so the relational operator < should be changed to <=, to include100. The if test is not necessary here, because counter is being incremented by 2, andwill always be even within the body of the do…while.
g) The following code should sum the integers from 100 to 150 (assume total is initial-ized to 0):
ANS: The semicolon at the end of the for statement should be removed, such that total+= x; is in the loop’s body.for ( x = 100; x <= 150; x++ ) /* ; removed */
total += x;
4.6 State which values of the control variable x are printed by each of the following for state-ments:
a) for ( x = 2; x <= 13; x += 2 )
printf( "%d\n", x );ANS: 2, 4, 6, 8, 10, 12b) for ( x = 5; x <= 22; x += 7 )
printf( "%d\n", x );ANS: 5, 12, 19c) for ( x = 3; x <= 15; x += 3 )
printf( "%d\n", x );ANS: 3, 6, 9, 12, 15
d) for ( x = 1; x <= 5; x += 7 )
printf( "%d\n", x );ANS: 1e) for ( x = 12; x >= 2; x -= 3 )
printf( "%d\n", x );ANS: 12, 9, 6, 3
4.7 Write for statements that print the following sequences of values:a) 1, 2, 3, 4, 5, 6, 7ANS:
for ( i = 1; i <= 7; i++ )
printf( “%d ”, i );
b) 3, 8, 13, 18, 23ANS:
/* increments of 5 */
for ( i = 3; i <= 23; i += 5 )
printf( “%d ”, i );
c) 20, 14, 8, 2, -4, -10ANS:
/* decrements of 6 */
for ( i = 20; i >= -10; i -= 6 )
printf( “%d ”, i );
d) 19, 27, 35, 43, 51ANS:
/* increments of 8 */
for ( i = 19; i <= 51; i += 8 )
printf( “%d “, i );
4.8 What does the following program do?
1 #include <stdio.h>23 /* function main begins program execution */4 int main( void )5 {6 int x;7 int y;8 int i;
9 int j;1011 /* prompt user for input */12 printf( "Enter two integers in the range 1-20: " );13 scanf( "%d%d", &x, &y ); /* read values for x and y */1415 for ( i = 1; i <= y; i++ ) { /* count from 1 to y */1617 for ( j = 1; j <= x; j++ ) { /* count from 1 to x */18 printf( "@" ); /* output @ */19 } /* end inner for */2021 printf( "\n" ); /* begin new line */22 } /* end outer for */2324 return 0; /* indicate program ended successfully */2526 } /* end function main */
ANS:
Enter integers in the range 1-20: 3 4@@@@@@@@@@@@
4.9 (Sum a Sequence of Integers) Write a program that sums a sequence of integers. Assume thatthe first integer read with scanf specifies the number of values remaining to be entered. Your pro-gram 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.ANS:
1 /* Exercise 4.9 Solution */2 #include <stdio.h>34 int main( void )5 {6 int sum = 0; /* current sum */
7 int number; /* number of values */8 int value; /* current value */9 int i; /* counter */1011 /* display prompt */12 printf( "Enter the number of values to be processed: " );13 scanf( "%d", &number ); /* input number of values */1415 /* loop number times */16 for ( i = 1; i <= number; i++ ) {
17 printf( "Enter a value: " );18 scanf( "%d", &value );19 sum += value; /* add to sum */20 } /* end for */2122 /* display sum */23 printf( "Sum of the %d values is %d\n", number, sum );24 return 0; /* indicate successful termination */25 } /* end main */
Enter the number of values to be processed: 5Enter a value: 10Enter a value: 15Enter a value: 20Enter a value: 25Enter a value: 30Sum of the 5 values is 100
4.10 (Average a Sequence of Integers) Write a program that calculates and prints the average ofseveral integers. Assume the last value read with scanf is the sentinel 9999. A typical input sequencemight be
10 8 11 7 9 9999
indicating that the average of all the values preceding 9999 is to be calculated.ANS:
1 /* Exercise 4.10 Solution */2 #include <stdio.h>34 int main( void )5 {6 int value; /* current value */7 int count = 0; /* number of values */8 int total = 0; /* sum of integers */910 /* display prompt */11 printf( "Enter an integer ( 9999 to end ): " );12 scanf( "%d", &value );1314 /* loop while sentinel value not read from user */15 while ( value != 9999 ) {16 total += value; /* update total */17 ++count;
1819 /* get next value */20 printf( "Enter next integer ( 9999 to end ): " );21 scanf( "%d", &value );22 } /* end while */2324 /* show average if more than 0 values entered */25 if ( count != 0 ) {26 printf( "\nThe average is: %.2f\n", ( double ) total / count );27 } /* end if */
28 else {29 printf( "\nNo values were entered.\n" );30 } /* end else */3132 return 0; /* indicate successful termination */33 } /* end main */
Enter an integer ( 9999 to end ): 1Enter next integer ( 9999 to end ): 2Enter next integer ( 9999 to end ): 3Enter next integer ( 9999 to end ): 4Enter next integer ( 9999 to end ): 5Enter next integer ( 9999 to end ): 6Enter next integer ( 9999 to end ): 9999
The average is: 3.50
4.11 (Find the Smallest) Write a program that finds the smallest of several integers. Assume thatthe first value read specifies the number of values remaining.
ANS:
1 /* Exercise 4.11 Solution */2 #include <stdio.h>34 int main( void )5 {6 int number; /* number of integers */7 int value; /* value input by user */8 int smallest; /* smallest number */9 int i; /* counter */1011 /* prompt user for number of integers */12 printf( "Enter the number of integers to be processed: " );13 scanf( "%d", &number );1415 /* prompt user for an integer */16 printf( "Enter an integer: " );17 scanf( "%d", &smallest );1819 /* loop until user has entered all integers */20 for ( i = 2; i <= number; i++ ) {21 printf( "Enter next integer: " ); /* get next integer */22 scanf( "%d", &value );23
24 /* if value is smaller than smallest */25 if ( value < smallest ) {26 smallest = value;27 } /* end if */2829 } /* end for */3031 printf( "\nThe smallest integer is: %d\n", smallest );32 return 0; /* indicate successful termination */
Enter the number of integers to be processed: 5Enter an integer: 372Enter next integer: 920Enter next integer: 73Enter next integer: 8Enter next integer: 3433
The smallest integer is: 8
4.12 (Calculating the Sum of Even Integers) Write a program that calculates and prints the sumof the even integers from 2 to 30.
ANS:
1 /* Exercise 4.12 Solution */2 #include <stdio.h>34 int main( void )5 {6 int i; /* counter */7 int sum = 0; /* current sum of integers */89 /* loop through even integers up to 30 */10 for ( i = 2; i <= 30; i += 2 ) {11 sum += i; /* add i to sum */12 } /* end for */1314 printf( "Sum of the even integers from 2 to 30 is: %d\n", sum );15 return 0; /* indicate successful termination */16 } /* end main */
Sum of the even integers from 2 to 30 is: 240
4.13 (Calculating the Product of Odd Integers) Write a program that calculates and prints theproduct of the odd integers from 1 to 15.
6 long i; /* counter */7 long product = 1; /* current product */89 /* loop through odd integers up to 15 */10 for ( i = 3; i <= 15; i += 2 ) {11 product *= i; /* update product */12 } /* end for */1314 printf("Product of the odd integers from 1 to 15 is: %ld\n", product );
15 return 0; /* indicate successful termination */16 } /* end main */
Product of the odd integers from 1 to 15 is: 2027025
4.14 (Factorials) The factorial function is used frequently in probability problems. The factorialof a positive integer n (written n! and pronounced “n factorial”) is equal to the product of the posi-tive 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 factorialof 20?
ANS: Calculating 20! is difficult in C because the resultant number is too large for even along int to store.
1 /* Exercise 4.14 Solution */2 #include <stdio.h>34 int main( void )5 {6 int i; /* outer counter */7 int j; /* inner counter */8 int factorial; /* current factorial value */910 printf( "X\tFactorial of X\n" ); /* display table headers */1112 /* compute the factorial for 1 to 5 */13 for ( i = 1; i <= 5; i++ ) {14 factorial = 1;1516 /* calculate factorial of current number */17 for ( j = 1; j <= i; j++ ) {18 factorial *= j;19 } /* end inner for */2021 printf( "%d\t%d\n", i, factorial );22 } /* end outer for */2324 return 0; /* indicate successful termination */25 } /* end main */
16 (Triangle Printing Program) Write a program that prints the following patterns separately,one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by asingle printf statement of the form printf( "*" ); (this causes the asterisks to print side by side).[Hint: The last two patterns require that each line begin with an appropriate number of blanks.]
1 /* Exercise 4.16 Solution */2 #include <stdio.h>34 int main( void )5 {6 int row; /* row counter */7 int col; /* column counter */8 int space; /* spaces counter */910 /* Pattern A, loop 10 times for rows */11 for ( row = 1; row <= 10; row++ ) {1213 /* print row asterisks */14 for ( col = 1; col <= row; col++ ) {15 printf( "*" );16 } /* end for */1718 printf( "\n" );19 } /* end for */2021 printf( "\n" );2223 /* Pattern B, loop 10 times for rows24 row counts down to correspond to number of asterisks */25 for ( row = 10; row >= 1; row-- ) {2627 /* print row asterisks */28 for ( col = 1; col <= row; col++ ) {29 printf( "*" );30 } /* end for */3132 printf( "\n" );33 } /* end for */3435 printf( "\n" );3637 /* Pattern C, loop 10 times for rows38 row counts down to correspond to number of asterisks */39 for ( row = 10; row >= 1; row-- ) {4041 /* print (10 - row) number of preceding spaces */42 for ( space = 1; space <= 10 - row; space++ ) {43 printf( " " );44 } /* end for */4546 /* print row asterisks */47 for ( col = 1; col <= row; col++ ) {48 printf( "*" );49 } /* end for */5051 printf( "\n" );52 } /* end for */
5354 printf( "\n" );5556 /* Pattern D, loop 10 times for rows */57 for ( row = 1; row <= 10; row++ ) {5859 /* print (10 - row) number of preceding spaces */60 for ( space = 1; space <= 10 - row; space++ ) {61 printf( " " );62 } /* end for */6364 /* print row asterisks */65 for ( col = 1; col <= row; col++ ) {66 printf( "*" );67 } /* end for */6869 printf( "\n" );70 } /* end for */7172 printf( "\n" );73 return 0; /* indicate successful termination */74 } /* end main */
4.19 (Calculating Sales) An online retailer sells five different products whose retail prices areshown in the following table:
Product numberRetail price
1$ 2.98
2$ 4.50
3$ 9.98
4$ 4.49
5$ 6.87
Write a program that reads a series of pairs of numbers as follows:a) Product numberb) 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.
ANS:
1 /* Exercise 4.19 Solution */2 #include <stdio.h>34 int main( void )5 {6 int product; /* current product number */7 int quantity; /* quantity of current product sold */8 double total = 0.0; /* current total retail value */
910 /* prompt for input */11 printf( "Enter pairs of item numbers and quantities.\n");12 printf( "Enter -1 for the item number to end input.\n" );13 scanf( "%d", &product );1415 /* loop while sentinel value not read from user */16 while ( product != -1 ) {17 scanf( "%d", &quantity );1819 /* determine product number and corresponding retail price */20 switch ( product ) {2122 case 1:23 total += quantity * 2.98; /* update total */24 break;2526 case 2:27 total += quantity * 4.50; /* update total */28 break;2930 case 3:31 total += quantity * 9.98; /* update total */32 break;3334 case 4:35 total += quantity * 4.49; /* update total */36 break;3738 case 5:39 total += quantity * 6.87; /* update total */40 break;4142 default:43 printf( "Invalid product code: %d\n", product );44 printf( " Quantity: %d\n", quantity );45 } /* end switch */4647 scanf( "%d", &product ); /* get next input */48 } /* end while */4950 /* display total retail value */51 printf( "The total retail value was: %.2f\n", total );5253 return 0; /* indicate successful termination */54 } /* end main */
Enter pairs of item numbers and quantities.Enter -1 for the item number to end input.1 12 13 14 15 16 1Invalid product code: 6
/* grade was uppercase A *//* grade was lowercase a */
26 ++aCount; /* update grade A counter *27break;/* exit switch */2829case 'B':/* grade was uppercase B */30case 'b':/* grade was lowercase b */
31 ++bCount; /* update grade B counter *32break;/* exit switch */3334case 'C':/* grade was uppercase C */35case 'c':/* grade was lowercase c */
36 ++cCount; /* update grade C counter *
4.22 (Average Grade) Modify the program of Fig. 4.7 so that it calculates the average grade forthe class.
ANS:
1 /* Exercise 4.22 Solution */2 #include <stdio.h>34 int main( void )5 {6 int grade; /* current grade */7 int aCount = 0; /* total a grades */8 int bCount = 0; /* total b grades */9 int cCount = 0; /* total c grades */10 int dCount = 0; /* total d grades */11 int fCount = 0; /* total f grades */12 double averageGrade; /* average grade for class */1314 /* prompt user for grades */15 printf( "Enter the letter grades.\n");16 printf( "Enter the EOF character to end input.\n" );1718 /* loop while not end of file */19 while ( ( grade = getchar() ) != EOF ) {2021 /* determine which grade was input */22 switch ( grade ) {23
92 return 0; /* indicate successful termination */93 } /* end main */
Enter the letter grades.Enter the EOF character to end input.A B B B C D FC C C D D D C BB A A B C C C^Z
The totals for each letter grade are:A: 3B: 6C: 8D: 4F: 1Average grade is C
4.24 Assume i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print?a) printf( "%d", i == 1 );ANS: 1b) printf( "%d", j == 3 );ANS: 0c) printf( "%d", i >= 1 && j < 4 );ANS: 1d) printf( "%d", m < = 99 && k < m );ANS: 0e) printf( "%d", j >= i || k == m );ANS: 1f) printf( "%d", k + m < j || 3 - j >= k );ANS: 0g) printf( "%d", !m );ANS: 0h) printf( "%d", !( j - m ) );ANS: 1i) printf( "%d", !( k > m ) );ANS: 0j) printf( "%d", !( j > k ) );ANS: 1
4.25 (Table of Decimal, Binary, Octal and Hexadecimal Equivalents) Write a program thatprints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range1 through 256. If you are not familiar with these number systems, read Appendix D before you at-tempt this exercise.
53 printf( "%d\t\t", number == 0 ? 0 : number );5455 /* hexadecimal numbers */56 number = loop;57 temp1 = 16;5859 if ( number < 4096 && number >= 256 ) {60 printf( "%d", number / 256 );61 number %= 16;62 } /* end if */6364 if ( number < 256 && number >= 16 ) {65 temp1 = number / 16;66 number %= 16;67 } /* end if */68 else {69 printf( "0" );70 } /* end else */7172 /* convert to letter if temp1 is above 9 */73 if ( temp1 <= 9 ) {74 printf( "%d", temp1 );75 } /* end if */76 else if ( temp1 >= 10 && temp1 <= 15 ) {77 printf( "%c", 'A' + ( temp1 - 10 ) );78 } /* end else if */79 else if ( temp1 == 16 ) {80 printf( "0" );81 } /* end else if */8283 /* convert to letter if number is above 9 */84 if ( number <= 9 ) {85 printf( "%d", number );86 } /* end if */87 else if ( number >= 10 && number <= 15 ) {88 printf( "%c", 'A' + ( number - 10 ) );89 } /* end else if */9091 printf( "\n" );92 } /* end for */9394 return 0; /* indicate successful termination */95 } /* end main */
4.31 (Diamond Printing Program) Write a program that prints the following diamond shape.You may use printf statements that print either a single asterisk (*) or a single blank. Maximizeyour use of repetition (with nested for statements) and minimize the number of printf statements.
****
************
*************************
ANS:
1 /* Exercise 4.31 Solution */2 #include <stdio.h>34 int main( void )5 {6 int line; /* line counter */7 int space; /* space counter */8 int asterisk; /* asterisk counter */9
10 /* top half */11 for ( line = 1; line <= 9; line += 2 ) {1213 /* print preceding spaces */14 for ( space = ( 9 - line ) / 2; space > 0; space-- ) {15 printf( " " );16 } /* end for */1718 /* print asterisks */19 for ( asterisk = 1; asterisk <= line; asterisk++ ) {20 printf( "*" );21 } /* end for */2223 printf( "\n" );24 } /* end for */2526 /* bottom half */27 for ( line = 7; line >= 0; line -= 2 ) {2829 /* print preceding spaces */30 for ( space = ( 9 - line ) / 2; space > 0; space-- ) {31 printf( " " );32 } /* end for */3334 /* print asterisks */35 for ( asterisk = 1; asterisk <= line; asterisk++ ) {36 printf( "*" );37 } /* end for */3839 printf( "\n" );40 } /* end for */4142 return 0; /* indicate successful termination */43 } /* end main */
4.32 (Modified Diamond Printing Program) Modify the program you wrote in Exercise 4.31 toread an odd number in the range 1 to 19 to specify the number of rows in the diamond. Your pro-gram should then display a diamond of the appropriate size.
ANS:
1 /* Exercise 4.32 Solution */2 #include <stdio.h>34 int main( void )5 {6 int line; /* line counter */7 int space; /* space counter */8 int asterisk; /* asterisk counter */9 int size; /* number of rows in diamond */1011 /* prompt for diamond size */12 printf( "Enter an odd number for the diamond size ( 1-19 ):\n" );13 scanf( "%d", &size );
1415 /* top half */16 for ( line = 1; line <= size - 2; line += 2 ) {1718 /* print preceding spaces */19 for ( space = ( size - line ) / 2; space > 0; space-- ) {20 printf( " " );21 } /* end for */2223 /* print asterisks */24 for ( asterisk = 1; asterisk <= line; asterisk++ ) {25 printf( "*" );26 } /* end for */2728 printf( "\n" );29 } /* end for */3031 /* bottom half */32 for ( line = size; line >= 0; line -= 2 ) {3334 /* print preceding spaces */35 for ( space = ( size - line ) / 2; space > 0; space-- ) {36 printf( " " );37 } /* end for */3839 /* print asterisks */40 for ( asterisk = 1; asterisk <= line; asterisk++ ) {41 printf( "*" );42 } /* end for */4344 printf( "\n" );45 } /* end for */4647 return 0; /* indicate successful termination */48 } /* end main */
Enter an odd number for the diamond size ( 1-19 ):13
****
************
********************
*************************************************
4.33 (Roman Numeral Equivalent of Decimal Values) Write a program that prints a table of allthe Roman numeral equivalents of the decimal numbers in the range 1 to 100.