To calculate the square root of a number in C++, you can make use of the sqrt() function from the cmath library. Here's an example:
``c++
#include
#include
using namespace std;
int main() {
double number, result;
cout << "Enter a number: ";
cin >> number;
result = sqrt(number);
cout << "The square root of " << number << " is " << result << endl;
return 0;
}
number
In this example, we first declare the variablesandresultof typedouble. We then prompt the user to enter a number which we store in thenumbervariable. We then calculate the square root of this number using thesqrt()function and store the result in theresultvariable. Finally, we output the result to the console usingcout.16For example, if the user enters
, the program will output:
The square root of 16 is 4
`
Note that the
sqrt() function returns a double, so we need to make sure to declare our variables as doubles in order to store the result. Also note that we need to include the cmath library to use the sqrt()` function.