C Program to find cube of a number
Objective
Write a C program to find the cube of a number.
Algorithm / Approach
- Declare two integers:
nandcb. - Read the number
nfrom the user. - Multiply the number by itself three times:
cb = n * n * n. - Print the calculated cube.
main.c
#include<stdio.h>
int main( ) {
int n, cb;
printf("Enter Value for N : ");
scanf("%d",&n);
cb = n*n*n;
printf("CUBE IS : %d\n",cb);
return 0;
}
Expected Output
Enter Value for N : 3 CUBE IS : 27
Explanation of the Program
- The cube of a number is the number multiplied by itself three times (n3).
- This program uses basic arithmetic operators (
*) to perform the calculation manually, which is the most efficient way to do it for simple integer powers.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)