c语言开方
程序1,用循环:
//---------------------------------------------------------------------------
#include <stdio.h>
long int st(int n)
{
long int srt=0;
int i;
for (i=1; i<=n; i++) {
srt+=2*i-1;
}
return srt;
}
int main(void)
{
long int s;
int n;
scanf("%d",&n);
s=st(n);
return 0;
}
//---------------------------------------------------------------------------
程序2,用递归:
//---------------------------------------------------------------------------
#include <stdio.h>
long int st(int n)
{
if (!n) return 0;
else return 2*n-1+st(n-1);
}
int main(void)
{
long int s;
int n;
scanf("%d",&n);
s=st(n);
return 0;
}
//---------------------------------------------------------------------------
C语言开方需要使用数学库函数sqrt(),该函数的功能是计算一个数的平方根。在使用该函数时,需要在代码中添加头文件#include <math.h>,然后通过调用sqrt()函数来计算所需数的平方根。
例如,要计算数x的平方根,可以使用double result = sqrt(x)来实现。需要注意的是,sqrt()函数只能计算正数的平方根,如果需要计算负数的平方根,需要将其转换为复数形式。
c语言开方怎么做
在 C 语言中,你可以使用 math.h 头文件中的 sqrt() 函数来计算一个数的平方根。下面是一个简单的例子:
c
#include <stdio.h>
#include <math.h>
int main() {
double num =
25.0;
double squareRoot = sqrt(num);
printf("The square root of %.2lf is %.2lf", num, squareRoot);
return 0;
}
在这个例子中,sqrt() 函数被用来计算变量 num 的平方根,并将结果存储在 squareRoot 变量中。
然后,printf() 函数被用来打印结果。
sqrt() 函数只接受非负数作为参数。如果你传入一个负数,它将返回一个 NaN(非数字)。如果你需要计算一个负数的平方根,你应该先将其转换为复数,然后使用 csqrt() 函数。例如:
c
#include <stdio.h>
#include <complex.h>
int main() {
double complex num =
25.0 -
20.0 * I;
double complex squareRoot = csqrt(num);
printf("The square root of %.2lf + %.2lfi is %.2lf + %.2lfi", creal(num), cimag(num), creal(squareRoot), cimag(squareRoot));
return 0;
}
在这个例子中,csqrt() 函数被用来计算复数 num 的平方根,并将结果存储在 squareRoot 变量中。
然后,printf() 函数被用来打印结果。

