C Program to Find the Square and Cube of a Number

This program takes a number as input from the user and calculates both its square and cube using arithmetic operations.

Concept Overview

The square of a number is obtained by multiplying the number by itself, and the cube is obtained by multiplying the number by itself twice more. In C, this can be done using simple multiplication operators.

Program

C
#include <stdio.h>

int main() {
    int num, square, cube;

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

    square = num * num;
    cube = num * num * num;

    printf("Square of %d is %d\n", num, square);
    printf("Cube of %d is %d\n", num, cube);

    return 0;
}

Sample Output

Enter a number: 4
Square of 4 is 16
Cube of 4 is 64

Explanation

  • `square = num * num;` – Calculates the square of the number.
  • `cube = num * num * num;` – Calculates the cube of the number.
  • `printf()` – Displays the results on the screen.
Note: Note: You can also use the `pow()` function from math.h, but simple multiplication is faster for integers.