Answer to Question #297782 in C++ for derano

Question #297782

Write a do while loop to require the user to enter two integers; the second integer must be equal to, or larger than, the first integer. Both integers must be at least 1 and not larger than 20. If they do not enter correct integers, give them error messages and make them do it again until they are correct.

After the acceptable integers have been entered, use a for loop to print a table of integers and their square roots for all integers from the first integer specified by the user to the last integer specified by the user, inclusive. Align the table and print 4 decimal positions

A sample table follows, for integers 7 to 9:

INTEGER  SQUARE ROOT
      7       2.6458
      8       2.8284
      9       3.0000
  

Test the program twice:

First test, first enter: first integer = 2, second integer = 0

and after that is rejected: first integer = 2, second integer = 4


Second test, first enter: first integer = 21, second integer = 5

and after that is rejected: first integer = 5, second integer = 5


1
Expert's answer
2022-02-14T15:04:23-0500


#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;


int main()
{
	int firstNum=-1;
	int secondNum=-1;
	do {
		cout << "Enter the first number [1-20]: " ;
		cin >> firstNum;
		if(firstNum<1 || firstNum>20){
			cout<<"Wrong integer value\n\n";
		}
	} while (firstNum<1 || firstNum>20);


	do {
		cout << "Enter the second number [1-20]: " ;
		cin >> secondNum;
		if(firstNum > secondNum || secondNum<1 || secondNum>20){
			cout<<"Wrong integer value\n\n";
		}
	} while (firstNum > secondNum || secondNum<1 || secondNum>20);
	cout << fixed;
	cout<<"INTEGER    SQUARE ROOT\n";
	for(int i=firstNum;i<=secondNum;i++){
		cout<<setw(7)<<i<<setw(15)<<setprecision(4)<<sqrt(i*1.0)<<"\n";
	}


	system("pause");
	return 0;
}

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