C Program to Find the Cube of a Number

In this article, we will write a C program to find the cube of a number. The program takes a number N from the user as input, calculates its cube, and prints the result on the output screen.

Sample Examples:

Input: N = 10
Output: cube = 1000
Explanation: Multiplying 10 * 10 * 10 = 1000

Input: N = 5
Output: cube = 125
Explanation: Multiplying 5 * 5 * 5 = 125

The cube of a number is the result of multiplying the number by itself three times. In mathematical terms, if you have a number x, the cube of x is denoted as x3 and is calculated as follows:

x3 = x * x * x;

For example, if x=2, then 23 (2 cubed) is calculated as 2x2x2, which equals 8. Similarly, if x=3, then 33 (3 cubed) is calculated as 3x3x3, which equals 27.

In C programming, you can find the cube of a number by using the multiplication operator(*).

Here is a simple example that calculates the cube of a number:

// C program to find the cube of a number
#include <stdio.h>

int main() {
    // Declare variables
    int number, cube;

    // Input: Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Calculate the cube of the number
    cube = number * number * number;

    // Output: Display the result
    printf("The cube of %d = %d", number, cube);

    return 0;
}

Output 1:

Enter a number: 10
The cube of 10 = 1000

Output 2:

Enter a number: 15
The cube of 15 = 3375

In this program, we have first declared two variables number and cube of type int. The program then asks the user to enter a number with the help of the printf() and scanf() functions.

The number entered by the user is stored in the number variable. On the next line, we calculate the cube of the number by multiplying number three times with itself.

The value of the cube of the number is stored in the cube variable.

Finally, the result is printed on the output screen using the printf() function.


2. Using Function to Calculate the Cube of a Number

To make our program more efficient and reusable, we can create a user-defined function to calculate the cube of the given number.

The user-defined function will take a single argument which will be the number itself and return the cube of the number as a result.

We can then call this function anywhere in our program whenever required.

See implementation in the below program:

// C program to find the cube of a number
#include <stdio.h>

// User defined function to find out the cube
int findCube(int number){
	int cube;
	
	// Calculate the cube of the number
	cube = number * number * number;
	
	// Return the cube
	return cube;
}

int main() {
	
    // Declare variables
    int number, cube;

    // Input: Get the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Call the function
    cube = findCube(number);

    // Output: Display the result
    printf("The cube of %d = %d", number, cube);

    return 0;
}

Output:

Enter a number: 6
The cube of 6 = 216

In this program, we have defined a custom function findCube(). This function takes a single argument of type int, calculates its cube, and returns the result back.

I hope you will find this article helpful. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment