Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#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);
}


What I have tried:

I have tried using parenthesis. Using 1.0/3.0 , 0.33
Posted
Updated 2-May-18 23:10pm

Using float variables with pow is pointless. Try
C
#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;
}
 
Share this answer
 
Comments
Zeeking99 4-May-18 11:52am    
Thanks Man it works. I remembered we can also use implicit casting.
Instead of (1.0/3) we can also use ((float)1/3).
Anyway Thanks for the hint.
It's pretty simple:
C++
b = pow(a, 1.0 / 3.0);
Or even
C++
b = pow(a, 1 / 3.0);
 
Share this answer
 
v2
Comments
Zeeking99 3-May-18 2:17am    
The program is still printing zero.
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);
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900