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 an ideal cube or not.
Test DataInput 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:
Flowchart:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming-exercises/numbers/c-numbers-exercise-29.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics