9.5k views
0 votes
NOTE: in mathematics, the square root of a negative number is not real; in C++ therefore, passing such a value to the square root function is an error.

Given a double variable named area Of Square write the necessary code to read in a value, the area of some square, into areaOfSquare and print out the length of the side of that square.

HOWEVER: if any value read in is not valid input, just print the message "INVALID".

1 Answer

3 votes

Answer:

Finding length of square side we will take help with Mathematics.

Below is the explanation.

Step-by-step explanation:

AreaOfSqure=x^2

x=
√(AreaOfSqure)

Code Example

#include<iostream> //for input and output

#include<tgmath.h> //for sqrt function

using namespace std;

int main()

{

double areaOfSquare;

cout<<"Enter area of square: ";

cin>>areaOfSquare;

if(areaOfSquare<=0){ //if entered value is less then 0

cout<<"INVALID";

}

else{

cout<<sqrt(areaOfSquare);

}

return 0;

}

Code Explanation

declaring double variable and asking user to input the area of square.

If the entered values i less then or equal to 0 then display invalid because sqrt formula will throw exception.

Otherwise calculate the length of side from calling sqrt function and printing the result by using cout.

Code Output

Enter area of square: 40

6.32456

User Claris
by
5.3k points