w3resource

C Exercises: Check whether a given number is a perfect cube or not

C Numbers: Exercise-29 with Solution

Write a program in C to check whether a given number is a perfect cube or not.

Test Data
Input a number: 125

Sample Solution:

C Code:

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


int main() 
{ 
int num, curoot,ans; 

 printf("\n\n Check whether a number is a perfect cube or not: \n");
 printf(" -----------------------------------------------------\n");
    printf(" Input a number: ");
    scanf("%d",&num);
curoot=round(pow(num, 1.0/3.0));

if(curoot*curoot*curoot==num)
{
  printf(" The number is a perfect Cube of %d \n",curoot);   
}
else
{
  printf(" The number is not a perfect Cube.\n");   
}
}

Sample Output:

 Input a number: 125                                                                                          
 The number is a perfect Cube of 5 

Pictorial Presentation:

C programming: Check whether a given number is a perfect cube or not.

Flowchart:

Flowchart: Check whether a given number is a perfect cube or not.

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C to find circular prime numbers upto a specific limit.
Next: Write a program in C to display first 10 Fermat numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?

It is a quirk of the syntax for passing arrays to functions.

Actually it is not possible to pass an array in C. If you write syntax that looks like it should pass the array, what actually happens is that a pointer to the first element of the array is passed instead.

Since the pointer does not include any length information, the contents of your [] in the function formal parameter list are actually ignored.

Ref : https://bit.ly/3fhlvdH