In this article, I will show you how you can write a C Program to find cube of a number using function. I will explain the algorithm, concepts and then help you to write C Program to find cube of a number using function.
1. Program Outline
In this program you’ll have to take a number as input from user and then calculate the cube and then print the cube of that number using function in terminal.
2. Concepts
To write this C program to find cube of number using function, you need to learn the following concepts of C Programming:
- Function
- Variables
3. Algorithm
Finding cube is very simple we just need to multiply the given number three times by itself. But in this program we are going to use function to find cube of given number.
To write this C program, follow this Algorithm:
Step 1: Input n
Step 2: create function findCube()
Step 3: cube = 1
Step 4: cube = nnn
Step 5: return cube
Step 6: call function findCube()
Step 7: Output cube
4. Program
Here’s the C Program to find Sum and Average of two numbers:
#include<stdio.h>
int findCube(int num){
// cube calculated and returned
return num * num * num;
}
int main() {
int num,cube;
printf("Enter any number: ");
scanf("%d",&num);
/* Cube function is called
to get cube of number */
cube = findCube(num);
printf("Cube of the given number is %d",cube);
return 0;
}
5. Output
Here’s the output of the above C Program:
