How do you square a number in the C language?

In C language, you can use the sqrt function from the math library to calculate square roots. You need to include the math.h header file in your program and use the sqrt function to calculate square roots. Here is a simple example:

#include <stdio.h>
#include <math.h>

int main() {
    double num, result;
    
    printf("请输入一个数字:");
    scanf("%lf", &num);
    
    result = sqrt(num);
    
    printf("该数字的平方根为:%lf\n", result);
    
    return 0;
}

In the above example, the user inputs a number, then the square root of that number is calculated using the sqrt function and printed out. It is important to note that the parameter type for the sqrt function is double, so the %lf format specifier is needed to read and print double type variables.

bannerAds