C++ Program to Find Area of a Square

C++ Program to Find Area of a Square

Calculating the area of a square is one of the simplest and most useful exercises for beginners in programming. In C++, this task introduces learners to variables, input/output operations, arithmetic calculations, and functions. Understanding how to compute the area of a square is important for applications in geometry, graphic design, construction, and other real-world scenarios.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Area of a Square Using User Input

This program allows the user to enter the length of a square’s side and calculates the area using the formula area = side × side. It is a simple way to practice basic arithmetic and I/O in C++.

#include <iostream>

using namespace std;

int main() {

    double side, area;

    cout << "Enter the length of the square's side: " << endl;
    cin >> side;

    area = side * side;

    cout << "The area of the square is: " << area << endl;

    return 0;

}

This program works by taking the user input for the side length, performing the multiplication, and displaying the result. Beginners learn how to manage variables, read input from the user, and output results effectively.

Program 2: Area Calculation Using a Predefined Value

In some cases, the side length of a square is known beforehand. This program demonstrates how to calculate the area using a predefined value, which is helpful for testing or automated calculations.

#include <iostream>

using namespace std;

int main() {

    double side = 7.0;  // predefined side length

    double area = side * side;

    cout << "For side length " << side << ", the area of the square is: " << area << endl;

    return 0;

}

Here, the program uses a constant for the square’s side. Beginners can focus on the arithmetic operation without dealing with input handling, which is a useful technique for debugging or examples.

Program 3: Using a Function to Calculate the Area

Using functions improves code reusability. This program defines a function to calculate the area of a square, which can be called with any side length.

#include <iostream>

using namespace std;

double calculateArea(double side) {
    return side * side;
}

int main() {

    double side;

    cout << "Enter the length of the square's side: " << endl;
    cin >> side;

    double area = calculateArea(side);
    cout << "The area of the square is: " << area << endl;

    return 0;

}

Using a function separates the calculation logic from the main program flow. Beginners learn to create modular, reusable code, making programs easier to read and maintain.

Program 4: Calculating Areas for Multiple Squares Using Vectors

When working with multiple squares, a vector can store side lengths and calculate areas for all of them efficiently.

#include <iostream>
#include <vector>

using namespace std;

double calculateArea(double side) {
    return side * side;
}

int main() {

    vector<double> sides {2.0, 4.5, 6.0};

    cout << "Calculating areas for multiple squares:" << endl;

    for(double side : sides) {

        double area = calculateArea(side);
        cout << "Side: " << side << " -> Area: " << area << endl;

    }

    return 0;

}

This program uses a vector to store multiple side lengths and a function to compute each area. Beginners can see how loops, vectors, and functions work together for larger datasets and practical calculations.

Frequently Asked Questions (FAQ)

Q1: Can I use integers instead of double for side lengths?
Yes, but using double allows for fractional side lengths, giving more precise results.

Q2: Why use functions for area calculation?
Functions make code reusable, organized, and easier to maintain, especially when calculating areas for multiple squares.

Q3: Can I calculate areas for user-defined multiple squares?
Yes, you can use a vector and a loop to input side lengths and calculate areas dynamically, similar to Program 4.

Q4: Can this method be applied to other shapes?
Absolutely! The same logic can be adapted for rectangles, circles, triangles, and more by changing the formula.

Conclusion

Finding the area of a square in C++ is an excellent exercise for beginners to practice variables, arithmetic, input/output, loops, vectors, and functions. Starting with user input, moving to predefined values, and finally using functions and vectors shows how programming scales from simple to reusable, structured code. Practicing these methods builds confidence and prepares learners for more complex geometric and mathematical programming challenges.

Additional & References

To continue learning, beginners should explore functions, vectors, and loops for geometry-based calculations. Practicing multiple approaches helps reinforce programming logic and real-world applications.

Scroll to Top