w3resource

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


29. Ideal Cube Check Variants

Write a program in C to check whether a given number is an ideal 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; // Declaring variables: 'num' for the input number, 'curoot' for the cube root, 'ans' for the answer

    // Printing information about the program and asking for user input
    printf("\n\n Check whether a number is a perfect cube or not: \n");
    printf(" -----------------------------------------------------\n");
    printf(" Input a number: ");
    scanf("%d", &num); // Reading the input number from the user

    curoot = round(pow(num, 1.0 / 3.0)); // Calculating the cube root using the 'pow' function

    // Checking if the cube of 'curoot' is equal to the input number 'num'
    if (curoot * curoot * curoot == num)
    {
        printf(" The number is a perfect Cube of %d \n", curoot); // Printing if the number is a perfect cube and displaying its cube root
    }
    else
    {
        printf(" The number is not a perfect Cube.\n"); // Printing if the number is not a perfect cube
    }
}

Sample Output:

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

Visual 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.

For more Practice: Solve these Related Problems:

  • Write a C program to check if a number is a perfect cube by computing its integer cube root.
  • Write a C program to verify ideal cubes using integer arithmetic to avoid floating-point errors.
  • Write a C program to test cube properties by comparing results from power functions and iterative methods.
  • Write a C program to determine if a number is an ideal cube by iterating through potential cube roots.

Go to:


PREV :> Circular Prime Numbers Up to a Limit Variants.
NEXT : Fermat Numbers Variants.

C Programming Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.