C++ Program for Area of Square

In this post, we are going to discuss one of the most basic programming questions. C++ program for area of the square. It may seem unnecessary because of its easy nature, but it is important and will help you if you work with large programs. Since it can be incorporated into those complex mathematical programming questions.

Additionally, it can also be used in problems involving numerical environments. 

Hence, without much delay, let’s get our coding brains to use and get started! 

What is a square?

We define a square as a closed two-dimensional shape with four equal sides and four equal angles. In addition, the four sides of the square form the four angles at the vertices.

Next, we move to the operations related to squares which are basically area and perimeter. We take the sum of the total length of the sides of a square as its perimeter. And, the total space occupied by the square is the area.

Some of the properties of a square are:

  • The opposite sides of a square are parallel.
  • Also, the four sides of a square are equal.
  • Each of the four angles of a square is 90 degrees. 

Area of a Square:

The area is the most popular term in geometry. In terms of geometry, we define the area of a square as the space occupied by it. If we go in-depth, the area is the number of square units needed to fill a square

In other words, we always consider the length of the side of a square while calculating the area of the square. Since all the sides are equal, the area of a square is the product of its two sides. The area of a square is a value we measure in terms of square units. For instance, square meters, square feet, square inch, etc.

Though we always prefer calculating the area of the square using its sides, there can be cases when we are not given the sides. In that case, we use other dimensions in a square. Like the diagonal and the perimeter of the square.

Formula to calculate the area of a square:

Since our program deals with the calculation of the area of a square, it will be better if we have a quick recap about it.

As we all know, the area of a square is the square of its sides. In other words, if we are given the sides of a square, we will calculate the area with the formula:

Area =(side)2

For example, if one side of a square is given as 10cm, its area = 10cm x 10cm = 100 square cm.

On the contrary, if we are given the diagonal of a square, the formula will change. 

Area = ((diagonal)^2)/2;

For example, if diagonal = 10cm , its area = (10cm x 10cm)/ 2 = 50 square cm. 

C++ program for area of square:

Just like our post for the sum of digits of a number and the program for matrix addition, we can approach this program in multiple ways.

The area of the square is nothing but a small block of code defined for one purpose only. Therefore, we always treat it as a sub-task of a particular big program file.

For instance, if you want to create a mathematical world about squares in which you calculate the area, perimeter, and other things. Here, you can use this area program as a function or a switch case attribute.

Hence, it may look ordinary, but this program has many purposes. 

Now onto the approaches, we have considered in this post. We will be calculating the area of a square in c++ using two methods for now. In the first one, we will not use any additional functions. Whereas, in the second, we will use a function for calculating the area.

C++ Program to calculate the area of square without using Function:

Algorithm:

  1. Start
  2. Declare the main function.
  3. Then, declare the area and side variable for area and side of the square respectively.
  4. After that, using cin and cout statements ask the user to enter the side of the square.
  5. Using the formula for calculating the area of square, calculate the area and store the value in the area variable.
  6. Finally, print the area as result.
  7. Stop.

Program:

// C++ program to calculate the area of square without using function
#include <iostream>
using namespace std;
int main() //main function for program execution
{
   int area, side;
   cout << "Enter the side of square:";
   cin >> side;
   area = side * side;
   cout << "Area of Square: " << area << endl;
   return 0;
} // main function end

Program Explanation:

The above set of codes need little or no explanation because of its simple nature. It is executing the basic statements in which we do the following:

First we define the main function with required variables.

Then, we take the input of the side of the square from the user.

After that using the formula of side x side, we calculate the area and store the value in area variable.

Finally, we display the area. 

Output:

The program we just executed gives the following output:

Output of c++ program for area of square without using function
Output C++ Program to calculate the area of square without using Function.

C++ Program to calculate the area of square without using Function:

Algorithm:

The program we are going to execute below involves an additional function. Its only function is to calculate the area of the square. This way, we will increase the efficiency of the program.

The algorithm is also simple. Thus, I hope you try to code the program on your own before going through the code given below. 

  1. Start.
  2. Declare a function int area(int side).
  3.    Then, In function area,
  4.       Declare a variable area and set it as side * side.
  5.       Finally, return area.
  6. Now, onto the main function,
  7. Take the user input for the side of the square.
  8. After that, pass the value of side to the area function.
  9. Finally, print the result. 
  10. Stop.

Program:

// C++ program to calculate the area of square using function
#include <iostream>
using namespace std;
//funcion to calculate area of square
int area(int side){
   int area = side * side;
   return area;
} // area function end
// main function or the driver function
int main(){
   int side;
   cout << "Enter the side of square:";
   cin >> side;
   cout <<"Area of square is :"<<area(side);
   return 0;
}// main function end

Program Explanation:

In the above program, we have just added a function to calculate the area. In all other terms, it is just like the previous method. 

At first, we have defined a function area(int side) which includes the formula side x side for calculating the area. 

Similarly, we have defined a main function. In this function, we have taken the user input for the side like before.

After that, we have passed the value of the side to the area function. The area function now calculates the area and returns it to the main function.

Finally, we have printed the value of area as the required result. 

Output:

The program we just executed gives the following output:

Output of C++ Program to calculate the area of square without using Function
Output of C++ Program to calculate the area of square without using Function.

Some Modifications:

Given the simplistic nature of the program, there can be many modifications to it. In other words, we can change the program in many ways to suit our purpose. This way, we have many approaches and with each approach, we improve the code for the better.

Some of the modifications that you can try are listed below:

  1. Since we have explained the occurrence of another formula above, you can include it in your code. Make a switch case or if-else statement so that the user can choose which formula to use.
  2. C++ provides the provision of a math library for calculating mathematical operations. You can use this library to calculate the square instead of manually typing it. In this way, the efficiency of the program will increase.
  3. We all know the different geometrical figures. So, why only settle for squares? Improve the program to include more shapes like circles, triangles, rectangles, etc.  
  4. Similarly, you can include perimeter as an extra value to calculate along with the area.

Ending note :

In conclusion, the area of a square is an easy but important concept to learn in coding. Additionally, this is one of those codes which is first taught at the school or college level. Learning this program and the modifications, you will be clear with the concept of area and other things such as perimeter and functions in c++.

Practice all these programs and you might find your own code! 

Still have doubts or queries? Just ask away. We are here to help.

See you in our next post!

Leave a Comment

Your email address will not be published. Required fields are marked *