C++ Program
What is the output of the following code fragment?(beta is of type int.)
beta = 5;
do
{
switch (beta)
{
case 1: cout <<'R'; break;
case 2: cout
case 4: cout << 'O'; break;
case 5: cout << 'L';
}
beta--;
}while (beta>1);
cout <<'X';
Statement inside case 2 of switch is missing in the question. Please provide the statement in a comment to get the correct answer. However, the answer below has been provided purely by assuming what the statement would have been.
CODE:
beta = 5; do { switch (beta) { case 1: cout << 'R'; break; case 2: cout << 'S'; break; case 4: cout << 'O'; break; case 5: cout << 'L'; } beta--; }while (beta>1); cout << 'X';
OUTPUT:
LOSX
EXPLANATION:
Iteration 1 of do-while loop: Here, beta = 5 and case 5 of switch will run and 'L' will get printed on the console. Now, beta-- will cause beta to become beta = 4 Iteration 2 of do-while loop: Here, beta = 4 and case 4 of switch will run and 'O' will get printed on the console. Now, beta-- will cause beta to become beta = 3 Iteration 3 of do-while loop: Here, beta = 3 and there is no case 3 in switch. So nothing will be printed on the console this time. Now, beta-- will cause beta to become beta = 2 Iteration 4 of do-while loop: Here, beta = 2 and case 2 of switch will run and 'S' will get printed on the console. Now, beta-- will cause beta to become beta = 1 Now, loop condition will become false and program control will come out of the loop as beta = 1. Finally, 'X' will be printed on the console. LOSX is printed on the console
FOR BETTER HELP PROVIDE THE MISSING STATEMENT AND PLEASE RATE THE ANSWER.
C++ Program What is the output of the following code fragment?(beta is of type int.) beta...
A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;
20) What is the output of the following segment of C code: int avg(int n, int* a); int main () { int array[4]={1,0,6,9}; printf("%d", avg(4, array)+ 1); system("pause"); return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...
2. What is the output of the following code fragment? n = 1; while (n <= 5) { n++; cout << n << ' '; a.1 2 3 4 5 b. 1 2 3 4 c. 1 1 1 forever d. 2 3 4 5 e. 2 3 4 5 6 3. What is the termination condition for the following While loop? while (beta > 0 && beta < 10) { cout << beta << endl; cin >> beta; }...
What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;
8. Given the following code fragment and function definition, what is(are) the output(s)? int funct (int n) int var = 1; while(n > 0) cout << funct(7)<<" "<<funct(0); var *= n; n--; return var;
3) What are the final values of a, b, c in the following code fragment (1.5 point): int a = 6 , b = 127 , c; c = ( ++a ) + ( b -- ); Answer: 4) What are the final values of a, b, c in the following code fragment (1.5 point): int a = 6 , b = 127 , c; c = (a++) + ( -- b); Answer: 5) What is displayed by this poorly...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 25; j > 16; j -= 3) cout << setw(5) << j; Answer: 2. int sum = 0; for (int k = -2; k <= 2; k++) sum = sum +...
Write the output from the following code fragment: for (int i = 1; I < 7; ++i) { switch (i) { case 1: printf ("\n%d Banana", i); case 2: printf ("\n%d Kumquat", i);
18 C++ 1. What is the output of the following program segment? int y-22: while ((y 3) != 0) cout << y<< "": y=y-2; The output is 2. Suppose that the input is 100, 20,-8, 50, 20. What is the output of the following C++ code? int sum0 int num: int j cin >> num: if (num < 0) continue зит зит + num; cout<< sum << endl; The output is
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) for (int k = 2; k < 5; k++) { for (int j = 3; j < 6; j++) cout << setw(4) << (k + j) << " "; cout << endl; }