C++ Program to Compute Square Root using math Library

This C++ program computes the square root of a number using math library. The program takes input from the standard input, computes the value and prints it.

Here is the source code of the C++ program computes the square root of a number using math library. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

/*

* C++ Program to Compute Square Root of a number using Standard Library

*/

#include <iostream>

#include <cmath>

#include <limits>

int main()

{

double output, input, min, max;

std::cout<<"Enter the value : ";

std::cin>> input;

std::cout<< std::endl;

min = std::numeric_limits<double>::min();

max = std::numeric_limits<double>::max();

if(input >= min && input <= max)

{

output =sqrt(input);

std::cout<<"Square root of "<< input

<<" = "<< output << std::endl;

}

else

{

std::cerr<<"Error: "<< input <<" not within limits"

<< std::endl;

}

return0;

}

advertisement

$ a.out
Enter the value : 1024.0
Square root of 1024 = 32

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement

If you wish to look at all C++ Programming examples, go to C++ Programs.