使用C中的pow函数查找数字的立方根。 [英] Finding cube root of a number using pow function in C.
本文介绍了使用C中的pow函数查找数字的立方根。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<stdio.h>
#include<math.h>
main()
{
float a,b;
printf("Enter a number : ");
scanf("%d",&a);
b = pow(a,(1/3));
printf("The cube root of a given number is : %f",b);
}
我的尝试:
我尝试过使用括号。使用1.0 / 3.0,0.33
What I have tried:
I have tried using parenthesis. Using 1.0/3.0 , 0.33
推荐答案
使用float
变量pow
毫无意义。尝试
Usingfloat
variables withpow
is pointless. Try
#include <stdio.h>
#include <math.h>
int main()
{
double a, b; // pow takes doubles
printf("Enter a number : ");
if ( scanf("%lf",&a) == 1 )
{
b = pow(a,(1.0/3));
printf("The cube root of %lf number is : %lf\n", a, b);
}
return 0;
}
这很简单:
It's pretty simple:
b = pow(a, 1.0 / 3.0);
甚至
b = pow(a, 1 / 3.0);
>
我明白了。而不是使用1/3或1.0 / 3.0,你应该使用0.333。你会得到正确的答案。
I got it. Instead of using 1/3 or 1.0/3.0 you should use 0.333. You will get the right answer.
#include<stdio.h>
#include<math.h>
main()
{
float a,b;
printf("Enter the number you want to find cube root of : ");
scanf("%f",&a);
printf("The number you entered is : %.2f\n",a);
b = pow(a,0.333);
printf("The cube root of the number = %.2f",b);
}
这篇关于使用C中的pow函数查找数字的立方根。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文