Answer to Question #315521 in C++ for nickname

Question #315521

This is a twist on the previous exercise that will help you review loops and decision structures. You will again ask the user to enter two numbers. However, you will ALWAYS subtract the smaller number from the larger number to ensure that you never get a negative number for an answer. You do this by checking the numbers and switching them if they are not in the right order (larger then smaller). All of this checking, switching, subtracting, and output of the answer should occur in a function.

 

Finally, ask the user if they would like to run the program again. By now, you should know exactly what type of loop to use.

 

Output:

 

Enter two integers (separated by a space) and this program will subtract the smaller from the larger:   [user enters: 7 5]

7 - 5 = 2

Do you want to run this program again?   [user enters: y]

Enter two integers (separated by a space) and this program will subtract the smaller from the larger:   [user enters: 5 7]

7 - 5 = 2

Do you want to run this program again?   [user enters: n]


1
Expert's answer
2022-03-22T09:07:06-0400
int main()
{
	int a,b;
	bool n = false;
	while (!n)
	{
		cout << "Enter two integers" << endl;
		cin >> a >> b;
		cout << a << " - " << b << " = " << a - b << endl;
		cout << "Do you want to run this program again? " << endl;
		cin >> n;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog