- In this program, we declare two double variables
num
andresult
. We useprintf()
andscanf()
to get the value of the number from the user. - We then use the
sqrt()
function to calculate the square root of the number. Thesqrt()
function takes one argument: the number for which we want to calculate the square root, and returns the result as a double precision floating point value. - Finally, we print out the result using
printf()
. Note that we use the%lf
format specifier to print out the double precision floating point value of the result.
#include <stdio.h> #include <math.h> int main() { double num, result; // Get the number from the user printf("Enter a number: "); scanf("%lf", &num); // Calculate the square root using the sqrt() function result = sqrt(num); // Print the result printf("The square root of %lf is %lf\n", num, result); return 0; }
OUTPUT
Enter a number: 16
The square root of 16.000000 is 4.000000
Menu