# C Program to find square of a number

Category: C Program

Learn how to write a C program to find the square of a number using a function. This article explains the concept, provides a complete code example, and highlights the benefits of using functions in C programming.

Calculating the square of a number is a fundamental mathematical operation, often encountered in various programming and real-world scenarios. The square of a number is obtained by multiplying the number by itself. In this article, we will explore how to write a C program that calculates the square of a given number using a dedicated function. We will provide a detailed explanation of the concept, the structure of the C program, and the implementation of the function to calculate the square.

## Understanding the Square of a Number

The square of a number n is denoted as n^2 and is calculated as:

n^2 = n x n

For example:

• The square of 2 is 2 x 2=4.
• The square of 5 is 5 x 5 = 25.

## Using Functions in C

Functions are a core concept in C programming. They allow us to encapsulate a specific piece of logic and reuse it throughout the program. A function in C consists of a function signature, a function body, and a return statement (if the function is not `void`). Functions help in breaking down a problem into smaller, manageable pieces, making the code more modular and readable.

## Write a C Program to find square of a number

We will write a C program that includes a function `square` to calculate the square of a number. The function will take an integer as an argument and return its square.

``````#include <stdio.h>

// Function to calculate the square of a number
int square(int num) {
return num * num;
}

int main() {
int number, result;

// Input the number from the user
printf("Enter a number to find its square: ");
scanf("%d", &number);

// Call the square function and store the result
result = square(number);

// Output the result
printf("The square of %d is %d.", number, result);

return 0;
}
``````

Output

``````Enter a number to find its square: 4
The square of 4 is 16.
``````

## Explanation of the Code

1. Function `square`:
• This function takes an integer `num` as a parameter and returns the result of `num * num`.
• The function calculates the square of the input number and returns it to the caller.
2. Main Function `main`:
• The program starts by declaring two integer variables: `number` and `result`.
• The user is prompted to enter a number, which is stored in the variable `number`.
• The function `square` is called with `number` as an argument, and the returned value is stored in `result`.
• The square of the entered number is then printed to the console.

## Benefits of Using Functions

• Modularity: Functions break down the code into smaller modules, making it easier to manage and understand.
• Reusability: Once defined, functions can be reused multiple times throughout the program, reducing code duplication.
• Maintainability: Functions make it easier to update and maintain the code, as changes in logic can be made in one place.