Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to find cube of a number

C Code Example — Basic Programs

ADVERTISEMENT

C Program to find cube of a number

Objective

Write a C program to find the cube of a number.

Algorithm / Approach

  1. Declare two integers: n and cb.
  2. Read the number n from the user.
  3. Multiply the number by itself three times: cb = n * n * n.
  4. 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)
ADVERTISEMENT