Square area calculation is a common problem for beginner-level programming. By writing this program, a beginner can learn how to take user input, how to calculate something, how to show output. In this article, we will show you the code and also going to explain that line-by-line.
Main Theory
Square area = `side^2` = `side\times side`
To calculate the area of the square we have to know the length of each side of the square. We can take that value from the user. Then we have to square that value. Finally, show the results to the user as an output.
Code
#include<stdio.h>
int main() {
int side;
int area;
printf("\nEnter the Length of Side : ");
scanf("%d", &side);
area = side * side;
printf("\nArea of the Square : %d", area);
return (0);
}
Output:
Enter the Length of Side : 5
Area of the Square : 25
Process returned 0 (0x0) execution time : 2.139 s
Press any key to continue.
Code Explanation
At first, we declare the header file. Then in the main function, we declare two integer type variables. Using scanf() we take the side length value from the user and then we store it inside the 'side' variable. Then we use the equation and store the result inside the area variable. Here is the simplified version of that formula:
Square area = `side^2` = `side\times side`
Print the area after calculating the area and storing the result inside the area variable. Using printf() you can print the result and show it to the user. That's how this program works.
Read more: C Program To Calculate The Area of A Circle
Conclusion
This is a beginner-friendly program. If you understand the full concept of this program then you can actually write another program like this. You just need to understand the formula and according to the formula, you can easily write the code. Always try to understand the code never try to memories it. Read our other articles about c programming and subscribe to our email subscription.