Question

Look at the following program: int main( ) {int exponent = 1; int three = 1;...

Look at the following program:

int main( )

{int exponent = 1;

int three = 1;

while (exponent <= 5)

{ three = three * 3;

cout << two << endl;

++exponent; }

return 0; }

(a) What is displayed?

(b) Change the while loop (and body of the loop) to a for loop.

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

int main()

{

                int exponent = 1;

                int three = 1;

                while (exponent <= 5)

                {

                                three = three * 3;

                                cout << three << endl;

                                ++exponent;

                }

                return 0;

}

(a) What is displayed?

Answer: The output of the above program is

3

9

27

81

243

Explanation: We have a variable three assigned with value 1.

Next we have have loop that runs from 1 to 5 each time multiply three by 3 and display it.

We have three = 1

Iteration 1: exponent = 1 (condition true)

                three = three * 3 => Now the value in three is 1 * 3 = 3, print it

Iteration 2: exponent = 2 (condition true)

                three = three * 3 => Now the value in three is 3 * 3 = 9, print it

Iteration 3: exponent = 3 (condition true)

                three = three * 3 => Now the value in three is 9 * 3 = 27, print it

Iteration 4: exponent = 4 (condition true)

                three = three * 3 => Now the value in three is 27 * 3 = 81, print it

Iteration 5: exponent = 5 (condition true)

                three = three * 3 => Now the value in three is 81 * 3 = 243, print it

Next exponent will be 6 and the loop terminates.

Hence the ouput is

3

9

27

81

243

Output of code using while loop

(b) Change the while loop (and body of the loop) to a for loop.

Answer:

int main()

{

                int exponent = 1;

                int three = 1;

                for(; exponent <= 5; exponent++)

                {

                                three = three * 3;

                                cout << three << endl;

                }

                return 0;

}

Output after modifying code to use for loop:

  

Add a comment
Know the answer?
Add Answer to:
Look at the following program: int main( ) {int exponent = 1; int three = 1;...
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
  • 1. What is wrong with the following C++ program? #include <iostream> int main() { a =...

    1. What is wrong with the following C++ program? #include <iostream> int main() { a = 4; b = 6; cout << a << "+" << b << "=" << a+b; return 0; 2. What is wrong with the following C++ program? What was its intended output? #include <iostream> using namespace std; int main() { cout << "What is larger? e pi or pi e?" << endl; double ans1 = exp(pi); double ans2 = pi exp(1.); cout << "epi is...

  • Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n...

    Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...

  • Can someone help me to convert from c++ to C program int main() { srand(time(0)); int...

    Can someone help me to convert from c++ to C program int main() { srand(time(0)); int playAgain; int PlayerScore=0; int computerScore= 0; int ties= 0; do { system("CLS"); Firstturn = rand()%(2-1+1)+1;// starting person. ComputerRandomPosition= rand()%(9-1+1)+1;// computer first pick- random gameWin=3; block1= '1'; block2= '2'; block3= '3'; block4= '4'; block5= '5'; block6= '6'; block7= '7'; block8= '8'; block9= '9'; //start of program cout<<"Welcome to Tic Tac Toe Game"<<endl<endl; cout<<"Player:"<<playerScore<<"Computer:"<<computerScore<<"Ties:"<<ties<<endl; if(Firstturn==1)// player first { cout<<"Please Select your a grid to place (X):"<<endl<<endl;...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • C++ What output does the following program produce? int getValue(int a, int b, int n); int...

    C++ What output does the following program produce? int getValue(int a, int b, int n); int main() cout << getValue(1, 7, 7) << endl; return 0; } // end main int getValue(int a, int b, int n) int returnValue = 0; cout << "Enter: a = " < a << " b = " << b << endl; int c = (a + b)/2; if (C* C <= n) return value = c; else returnValue = getValue(a, C-1, n); cout...

  • Given the following program segment: int limit = 4; int first = 5 int j; for...

    Given the following program segment: int limit = 4; int first = 5 int j; for (j=1; j <= limit; j++) { cout << first * j << endl; first = first + (j -1); } cout << endl; write a while loop and a do...while loop that both have the same output.

  • What is the output of the following program? int main(void) { int x, y, z; x...

    What is the output of the following program? int main(void) { int x, y, z; x = 5; y= test(x); z= x + y; cout<< setw(4)<<x <<” + “<<setw(4)<<y<<”=” << setw(4)<<z<<endl; return 0; } int test (int x) { int w; w = x + 10; return (w); }

  • Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout...

    Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;

  • #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating...

    #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating odd numbers, init to 0    int sumEven = 0; // For accumulating even numbers, init to 0    int upperbound; // Sum from 1 to this upperbound    // Prompt user for an upperbound    cout << "Enter the upperbound: ";    cin >> upperbound;    // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound    int number =...

  • Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int...

    Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...

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