C Program to Calculate Cube of a Number

Howdy readers, today you will learn how to write a program to calculate the cube of a number using C Programming language.

C Program to Calculate Cube of a Number

When an integer is multiplied by itself three times the resultant number is known as its cube number. In simple words, a cube number is a number obtained by the product of three same numbers. 

We will compute the cube of a number using the following methods:

  1. Using Standard Method
  2. Using pow() Function
  3. Using User-defined Function

So, without any delay, let’s begin this tutorial.

C Program to Calculate Cube of a Number

C Program

// C Program to Calculate Cube of a Number
#include <stdio.h>
int main(){
    int num, cube;
    
    // Taking input
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    // Calculating Cube
    cube = num * num * num;
    
    // Displaying result
    printf("Cube of %d is: %d", num, cube);
    
    return 0;
}

Output

Enter an integer: 9
Cube of 9 is: 729
C Program to Calculate Cube of a Number Using Standard Method

Explanation

    int num, cube;

We have declared two int data type variables named num and cube.

    // Taking input
    printf("Enter an integer: ");
    scanf("%d", &num);

After that, the user is asked to enter a positive integer to find its cube. This number gets stored in the ‘num’ named variable.

    // Calculating Cube
    cube = num * num * num;

The cube of the entered number is computed by multiplying the same number three times. The result gets stored in the cube named variable.

    // Displaying result
    printf("Cube of %d is: %d", num, cube);

The cube of the number obtained in the previous step is displayed on the screen using printf() function.

C Program to Calculate Cube of a Number Using Pow() Function

C Program

// C Program to Calculate Cube of a Number Using pow() Function
#include <stdio.h>
#include <math.h>

int main(){
    int n, cube;
    
    // Taking input
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    
    // Calculating cube using pow()
    cube = pow(n, 3);
    
    // Displaying result
    printf("Cube of %d is: %d", n, cube);
    
    return 0;
}

Output

Enter a positive integer: 11
Cube of 11 is: 1331
C Program to Calculate Cube of a Number Using pow() function

Explanation

    // Calculating cube using pow()
    cube = pow(n, 3);

The pow() function takes two arguments (the base, and the exponent) and returns the power raised to that value. The pow() function is defined in the math.h header file.

C Program to Calculate Cube of a Number Using User-defined Function

C Program

// C Program to Calculate Cube of a Number Using User-defined Function
#include <stdio.h>
int cubeNumber(int);

int main(){
    int num, cube;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Calling out user-defined function
    cube = cubeNumber(num);
    
    // Displaying result
    printf("Cube of %d is: %d", num, cube);
    
    return 0;
}

int cubeNumber(int n){
    return (n * n * n);
}

Output

Enter a number: 6
Cube of 6 is: 216
C Program to Calculate Cube of a Number Using Function

Explanation

int cubeNumber(int n){
    return (n * n * n);
}

In the above program, we have defined a custom function named cubeNumber which returns the cube value using simple multiplication.

Conclusion

I hope after reading this tutorial, you will understand how to write a program to calculate the cube of a number using C Programming language.

Don’t forget to comment down your queries in the comment section, we will be pleased to solve all of your doubts.

Leave a Comment