C Program To Find Cube of a Number

In this post, we will learn how to find cube of a number using the C Programming language.

C Program To Find Cube of a Number

When a number is multiplied three times by itself, the product obtained is called the cube of a number. For example: Cube of 3 is 3 x 3 x 3 = 27.

We will be using two techniques to find the cube, first by using a standard method and second by using a user defined function.

So, without further ado, let’s begin the tutorial.

C Program To Find Cube of a Number

// C Program To Find Cube of a Number 
#include <stdio.h>
int main(){
    int num, cube;
    
    // Asking for Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    cube = num * num * num;
    printf("Cube of %d is %d", num, cube);
    return 0;
}

Output

Enter an integer: 5
Cube of 5 is 125

How Does This Program Work ?

    int num, cube;

In this program, we have declared two int data type variables named num and cube

    // Asking for Input
    printf("Enter an integer: ");
    scanf("%d", &num);

Then, the user is asked to enter an integer. The value of this number will get stored in the num named variable. 

    cube = num * num * num;

The cube of the number is obtained by multiplying the same number by itself three times . 

    printf("Cube of %d is %d", num, cube);

Finally, the cube of the number is printed using printf() function.

C Program To Find Cube of a Number Using Function

// C Program To Find Cube of a Number Using Function
#include <stdio.h>

int cube(int x){
    return x * x * x;
}

int main(){
    int num, result;
    
    // Asking for Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    result = cube(num);
    printf("Cube of %d is %d", num, result);
    
    return 0;
}

Output

Enter an integer: 7
Cube of 7 is 343
int cube(int x){
    return x * x * x;
}

In this program, we have declared a user defined function named as cube which returns the product of the number multiplied three times by itself. 

Then, this custom function is called in the main function whenever needed to calculate the cube of a number.

Conclusion

I hope after going through this post, you understand how to find a cube of a number using C Programming language.

If you have any doubt regarding the problem, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *