C Program to Find Square of a Number

C Program to Find Square of a Number

In this article, we are going to discuss how to calculate the square of a number.

For example –

Input-
the first step we take user input in this program so for that we take suppose 22.

Logic –
To calculate the square of a given number we required the formula and the formula is :
square = number * number;

Output –
According to our input, we get output as

Enter the number:22
The square of 22.000000 is: 484.000000

So let’s start to implement these given formulas in our program.

//C program to calculate the square of number

#include<stdio.h>
int main()
{
float number, square;
printf(“Enter the number :”);
scanf(“%f”, &number);
square = number * number;
printf(“The square of %f is : %f”, number, square);
return 0;
}

output

Enter the number:22
The square of 22.000000 is: 484.000000

From the above program, we can see that, by using a formula we simply calculate the square of a number. But now we are going to calculate the square of numbers by using user define a function, so let’s start.

//C program to calculate the square of the number

#include<stdio.h>
float square(float number){
return number * number;
}
int main()
{
float number;
printf(“Enter the number :”);
scanf(“%f”, &number);
printf(“The square of a number is : %f”,square(number));
return 0;
}
Output

Enter the number:12
The square of a number is: 144.000000

From the above program, we can see that in this program we simply use the user to define a function in it.

By using these functions we simply return the output of our by using these function.

So these are the two methods, by using these we can simply calculate the square of any number. I hope these methods understand you. You can also explore these programs by using another value. So for now keep learning, and keep exploring!