Program in C to Find Cube of a Number

Program in C to Find Cube of a Number

Finding the cube of a number means multiplying the number by itself three times. This program demonstrates how to calculate the cube of a number using multiple methods in C, including direct multiplication, using a function, and using the pow() function from the math library.

Understanding The Problem

The program should take a number as input from the user and output its cube. The cube is calculated by multiplying the number by itself twice more, or by using a library function. Handling both integer and decimal inputs ensures the program is flexible.

Steps to Solve the Problem:

  • Take input from the user.
  • Calculate the cube using one of several methods.
  • Display the result.

Solution 1: Using Direct Multiplication

This method multiplies the number by itself twice to get the cube.

#include <stdio.h>

int main() {

    int number, cube;

    printf("Enter an integer: ");
    scanf("%d", &number);

    cube = number * number * number;

    printf("Cube of %d is %d\n", number, cube);

    return 0;

}

The program asks the user for a number, multiplies it by itself three times, and prints the result. This is the simplest and fastest method for integer inputs.

Solution 2: Using a Function

Using a function improves readability and allows reuse of the cube calculation in multiple parts of a program.

#include <stdio.h>

double cubeNumber(double num) {
    return num * num * num;
}

int main() {

    double number;

    printf("Enter a number: ");
    scanf("%lf", &number);

    printf("Cube of %.2lf is %.2lf\n", number, cubeNumber(number));

    return 0;

}

The cubeNumber() function takes the input and returns the cube, making the main program cleaner and easier to read.

Solution 3: Using the pow() Function

The pow() function from <math.h> can be used to calculate any power, including the cube.

#include <stdio.h>
#include <math.h>

int main() {

    double number, cube;

    printf("Enter a number: ");
    scanf("%lf", &number);

    cube = pow(number, 3);

    printf("Cube of %.2lf is %.2lf\n", number, cube);

    return 0;

}

Using pow(number, 3) calculates the cube directly. This method is useful when you want a general solution for powers beyond 3.

FAQs

Q1: Can we find cubes of negative numbers?
Yes, the cube of a negative number is also negative because multiplying an odd number of negatives results in a negative.

Q2: Which method is best?
Direct multiplication is fastest and simplest, functions improve readability and reusability, and pow() is flexible for any power calculation.

Q3: Can we find cubes of decimal numbers?
Yes, using double or float types handles decimals accurately.

Conclusion

This program demonstrates how to calculate the cube of a number using different methods in C. Direct multiplication is the simplest, using functions improves code structure, and pow() provides flexibility for more general power calculations.

References & Additional Resources

Scroll to Top