Question

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; coutOutput: 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 the program do? Write a general statement that captures the computation being performed by the program. I. 2.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

Output for value of n are mentioned below

-1 => None

0 => None

1 => 0

2 => 1

3 => 1

4 => 2

5 => 2

6 => 2

7 => 2

8 => 3

9 => 3

Please eater 5411otly positive suber Progren Eini.shneB wi จ exit ood. 0 Progzan fintshed with axie ade 1 utpati Progran tini

Answer 2:

This program basically counts the number of iteration until a division of user input value by 2 is greater than 1. If user input value is less than or equal to 0 then program exit without any error.

This program will first initialize two integer variables i and n as value zero.

Then program will ask to enter positive integer.

If user enters value which is less than or equal to 0 then the program exits without any error.

After that program counts the number of iteration until a division of user input value by 2 is greater than 1. For example if user enters 9 then program sets 9 into variable n.

In while loop it satisfies the condition and iterates for 3 times until the division of n became 1 so after 3rd iteration while loop condition failed to satisfy so loop terminates.

Then program prints the result of i as 3.

Add a comment
Know the answer?
Add Answer to:
Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n...
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
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
ADVERTISEMENT