Q5 8-57 Flashcards
Predict the output of this program fragment: i = 0; while(i , i, 10 - i); i = i + 1; }
- •0 ••10
- •1 ••9
- •2 ••8
- •3 ••7
- •4 ••6
What is displayed by this program fragment for an input of 8?
scanf("%d", &n); ev = 0; while (ev < n){ printf("%3d", ev); ev = ev + 2 ; } printf("\n");
••0••2••4••6
What output values are displayed by the following while loop for a data value of 5? Of 6? Of 7? In general, for a data value of any number n, what does this loop display?
printf(“Enter an integer> “);
scanf(“%d”, &x);
product = x;
count = 0;
while (count
(5): 5 25 125 625
It lists the powers of given integer from 1 to 4.
What values are displayed if the call to printf comes at the end of the loop instead of at the beginning? (Pertains to previous question)
It will list the powers of the given integer from 2 to 5.
The following segment needs revision. Insert braces where they are needed and correct the errors. The corrected code should take five integers and display their sum.
count = 0;
while (count , sum);
int count = 0, next_num, sum = 0;
while (count , sum);
Write a program segment that computes 1 + 2 + 3 + … + (n - 1 ) + n , where n is a data value . Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. What message do you think will he displayed?
int n, sum=0, count=1;
printf(“Input number to add to: “);
scanf(“%d”, &n);
for(; count , n, sum);
}
}
Trace the execution of the loop that follows for n = 8. Show values of odd and sum after the update of the loop counter for each iteration.
sum = 0;
for (odd = 1; odd
ODD: 1, 3, 5, 7
SUM: 1, 4, 9, 16
Sum of positive odd numbers less than 8 is 16.
Consider the following definitions: #define CBEGIN 10 #define CLIMIT -5 #define CSTEP 5 indicate what values of celsius would be at the end of the following for loops: for(celsius = CLIMIT; celsius = CBEGIN; celsius += CSTEP); for(celsius = CSTEP; celsius >= CBEGIN; celsius += CLIMIT) ; for(celsius = CLIMIT; celsius
1.
Rewrite the code shown in the previous question so the effect is equivalent but no increment/decrement operator appears in an expression with another arithmetic operator.
x
What errors do you see in the following fragment? Correct the code so it displays all multiples of 4 from 0 through 100.
for mult4 = 0;
mult4
for(mult4=0; mult4, mult4);
}
Trace the following program fragment:
j = 10;
for(i = 1; i , i, j);
j -= 2;
}
1 10 2 8 3 6 4 4 5 2
Rewrite the previous program fragment so that it produces the same output but uses 0 as the initial value of i.
j = 10; for(i = 0; i < 5; ){ printf("%d %d\n", ++i, j); j -= 2; }
Write a program to display a centimeters-to-inches conversion table. The smallest and largest number of centimeters in the table are input values. Your table should give conversions in 10-centimeter intervals. One centimeter equals 0.3937 inch.
x
There are 9,870 people in a town whose population increases by 10 percent each year. Write a loop that displays the annual population and determines how many years it will take for the population to surpass 30,000.
int pop=9870, count=0;
float perc=1.10;
while(pop, count);
What is displayed by the following program segments? m = 3; n = 5; for(i = 1; i < i; ++j){ printf("*"); } printf("\n"); }
*
- *
- **