Table of contents
1.
Introduction
2.
C++ Program to find the cube of a number using functions
2.1.
Syntax
3.
Examples
3.1.
Example 1
3.2.
Example 2
4.
Implementation in C++
4.1.
Time Complexity: O(1)
4.2.
Space Complexity: O(1) 
5.
Frequently Asked Questions
5.1.
What is Time Complexity?
5.2.
Can you find the cube of a number using the pow() function?
5.3.
How is the space complexity O(1) in this program?
6.
Conclusion
Last Updated: Mar 4, 2025
Easy

C++ Program to find the cube of a number using functions

Author SHIVAM SINHA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Cube is the result of multiplying an integer by itself three times. We can find out the cube of a number by implementing our function, the function will find the cube by multiplying the number three times, or we can find the cube of any number by using the pow() function, which is predefined in the cmath header file.

C++ Program to find the cube of a number using functions

To get the cube of a number, we will define a function in C++. We will declare a function prototype by giving the data type, name, and the number of parameters. Here the function needs only one parameter to find the cube. Now, we will define the function with one parameter. The function's return type will be an integer as the function will return the cube of the given number.

Syntax

return_type function_name(data_type parameter...)
You can also try this code with Online C++ Compiler
Run Code

Examples

Now, let’s understand this with the help of examples.

Example 1

Input: 5

Output: 125

Suppose we have the number 5, and we have to find the cube. To find the cube of 5, we have to multiply it three times, i.e., 5*5*5, equal to 125.

Example 2

Input: 10

Output: 1000

Now, Suppose we have the number 10, and we have to find the cube. To find the cube of 10, we have to multiply it three times, i.e., 10*10*10, which equals 1000.

Now let’s see the C++ implementation of this approach.

Implementation in C++

C++ Program

#include <iostream>
#include <cmath>
using namespace std;
int cube(int a)
{
return(a*a*a);
}

int main()
{
   
           int number;
           cout << "ENTER THE NUMBER:";
           cin >> number;
           int res=cube(number);
           cout << "cube obtained:" << res; //calling function
           return 0;
}
You can also try this code with Online C++ Compiler
Run Code

We have created a cubic function in this program, which takes a number as an argument and returns an integer. We are asking for a number as input from the user, and we are calling our function bypassing the user’s input, and then the function returns the cube of the number, and then we are just printing the result.

Time Complexity: O(1)

Space Complexity: O(1)
 

Try and compile by yourself with the help of online C++ Compiler for better understanding.

Frequently Asked Questions

What is Time Complexity?

The amount of time taken by an algorithm for its complete execution is called the time complexity of the algorithm. It shows the efficiency of an algorithm. To know more about the time complexity, click here.

Can you find the cube of a number using the pow() function?

Yes, we can also find the cube of a number using the pow() function defined in the cmath header file.

How is the space complexity O(1) in this program?

The above function takes the same amount of space regardless of the input size.

Conclusion

In this article, we have discussed the C++ program to find the cube of a number using a function. We also discussed the time complexity and space complexity of the program.

Recommended Readings:

Live masterclass