Square in C : Calculate square of a number

Square in C
Square in C

Problem Statement: Given a number N, calculate the square of the number N. Square in C.

Examples:

Example 1:
Input: N = 3
Output: 9
Explanation: 3*3 = 9

Example 2:
Input: N = 4
Output: 16
Explanation: 4*4 = 16

DisclaimerDon’t jump directly to the solution, try it out yourself first.

What is Square of a Number?

A square of a number is simply the resultant number obtained by multiplying the number with itself. For example, square of 2 will be 2*2, and square of 10 will be 10*10 and so on.

Approach:

To calculate the square of a number we need to multiply the number by itself.

Code:

C Program

#include<stdio.h>
int square(int n)
{
    int sq = n*n;
    return sq;    
}

int main()
{
    int n=3;
    printf("The square of the given number %d is %d",n,square(n));
}

Output:

The square of the given number 3 is 9

Time Complexity: O(1)

Space Complexity: O(1)

Special thanks to Subhrajit Das for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article