Question

C++ Program What is the output of the following code fragment?(beta is of type int.) beta...

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';

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
C++ Program What is the output of the following code fragment?(beta is of type int.) beta...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A. What is the output of the following C++ code fragment? (all variables are of type...

    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*...

    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;...

  • 1. What would be the output of the following lines of code: int num = 2;...

    1. What would be the output of the following lines of code: int num = 2; switch(num){ case 1: cout << "1"; case 2: cout << "2"; case 3: cout << "3"; case 4: cout << "4"; } 2. What would be the output of the following code: char op = '*'; switch(op){ case '+': cout << "Addition"; break; case '-': cout << "Subtraction"; break; case '/': cout << "Division"; break; default: cout << "Invalid"; } 3. What would be...

  • 2. What is the output of the following code fragment? n = 1; while (n <=...

    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 <=...

    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...

    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...

    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...

    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 +...

  • 1)What is the output of the following code: int i = 10; do { cout <<...

    1)What is the output of the following code: int i = 10; do { cout << i; i++; } while (i < 5); cout << "-done"; 2)What is the output of the following code (note there are no endl's, all output is on single line)? for (int i = 1; i <= 3; i++) {    cout << '(';    for (int j = 0; j < (i*2); j++)        cout << 'x';    cout << ')'; }

  • Write the output from the following code fragment: for (int i = 1; I < 7;...

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

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT