C Program to Find Square and Cube of a Number

Howdy readers, today you will learn how to write a program to find the square and cube of a number using the C Programming language.

C Program to Find Square and Cube of a Number

When a number or an integer is multiplied by itself, the resultant is called a ‘Square’ number. For example, the square of 7 is 7 x 7 = 49.

Similarly, when a number or an integer is multiplied by itself three times the resultant number is known as the ‘Cube’ of the number. For example, the cube of 3 is 3 x 3 x 3 = 27.

The below program prompts the user to enter an integer, then computes square and cube of the entered number using the following methods:

  1. Using Standard Method
  2. Using User-defined Function

So, without any delay, let’s begin this tutorial.

C Program to Find Square and Cube of a Number

C Program

// C Program to Find Square and Cube of a Number
#include <stdio.h>
int main(){
    int num, square, cube;
    
    // Taking input
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    // Finding Square
    square = num * num;
    
    // Finding Cube
    cube = num * num * num;
    
    // Displaying result
    printf("Square of %d is: %d \n", num, square);
    printf("Cube of %d is: %d", num, cube);
    
    return 0;
}

Output

Enter an integer: 5
Square of 5 is: 25 
Cube of 5 is: 125
C Program to Find Square and Cube of a Number

Explanation

    int num, square, cube;

In the above program, we have declared three int data type variables named num, square and cube.

    // Taking input
    printf("Enter an integer: ");
    scanf("%d", &num);

The user is asked to enter an integer. This number gets stored in the num named variable.

    // Finding Square
    square = num * num;

Square of the entered number is computed by multiplying the number by itself. The result gets stored in the square named variable.

    // Finding Cube
    cube = num * num * num;

Similarly, the cube of the entered number is calculated by multiplying the number three times. The result gets stored in the cube named variable.

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

The square and cube of the entered number is printed on the screen using printf() function. Here, the (\n) is a newline character used to jump to next lines.

C Program to Find Square and Cube of a Number Using Functions

C Program

// C Program to Find Square and Cube of a Number Using Functions
#include <stdio.h>

int findSquare(int n){
    return n*n;
}

int findCube(int n){
    return n*n*n;
}

int main(){
    int num, square, cube;
    
    // Taking input
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    // Calling out function to find square
    square = findSquare(num);
    
    // Calling out function to find cube
    cube = findCube(num);
    
    // Displaying result
    printf("Square of %d is: %d \n", num, square);
    printf("Cube of %d is: %d", num, cube);
    
    return 0;
}

Output

Enter an integer: 7
Square of 7 is: 49 
Cube of 7 is: 343
C Program to Find Square and Cube of a Number Using Functions

Explanation

int findSquare(int n){
    return n*n;
}

In the above program, we have defined a custom function named findSquare which computes and returns the square of a number using simple multiplication.

int findCube(int n){
    return n*n*n;
}

Similarly, we have defined another user-defined function named findCube which calculates and returns the cube of a number.

Conclusion

I hope after reading this tutorial, you understand how to write a program to find the square and cube of a number using the C Programming language.

If you have any queries regarding the tutorial, then let us know in the comment section. We will be pleased to solve all of your queries.

Leave a Comment