Answer:
See explaination
Explanation:
// Include the necessary header files.
#include <iostream>
#include <exception>
#include <cmath>
using namespace std;
// Definition of the function.
int square_root(int n)
{
// Check whether n is less than 0.
if(n < 0)
// Throw exception.
throw domain_error("Supplied integer is negative!");
// check whether n is equal to 0;
else if(n == 0)
// Throw 0.
throw 0;
// Declare a variable and assign value.
double squ_root = sqrt(n);
// // Declare a variable and assign value.
int squ_root_int = (int)squ_root;
// compare values
// check whether the values are equal or not.
if(squ_root != squ_root_int)
// Throw exception.
throw invalid_argument("Supplied integer is not a perfect square !");
// return the value.
return squ_root_int;
}
// Declare the main function.
int main()
{
// declare variables.
int n;
int squ_root;
// Prompt the user to enter the number.
cout << "Enter a number: ";
cin >> n;
// start the try block.
try
{
// call to the function.
squ_root = square_root(n);
// Display the statement on console.
cout << "Square root is " << squ_root << endl;
}
// start the catch block.
catch(domain_error e)
{
// Display the statement on console.
cout << "Error: " << e.what() << endl;
return -1;
}
// Start the catch block
catch (int y)
{
// check whether y is equal to zero or not.
if(y == 0)
{
// Display the statement on console.
cout << "Error: Supplied integer is zero !" << endl;
return -2;
}
}
// Start the catch block
catch(invalid_argument e)
{
// Display the sstatement.
cout << "Error: " << e.what() << endl;
return -3;
}
// Return the value.
return 0;
}