C++ Program to Find Perimeter of a Square

C++ Program to Find Perimeter of a Square

Calculating the perimeter of a square is a foundational exercise in geometry and programming. The perimeter is the total length around a square and can be easily calculated using the formula P = 4 × side. Learning to implement this in C++ helps beginners practice arithmetic operations, input/output handling, functions, and arrays or vectors, which are essential for building strong programming skills.

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: Perimeter Using User Input

This program allows the user to enter the length of the square’s side and calculates the perimeter. It’s a simple example to practice basic input, arithmetic, and output in C++.

#include <iostream>

using namespace std;

int main() {

    double side, perimeter;

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

    perimeter = 4 * side;

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

    return 0;

}

This program works by multiplying the entered side length by four to get the perimeter. Beginners can see how user input directly affects calculations and how formulas translate into code.

Program 2: Perimeter Using Predefined Side

If the side length is known beforehand, we can use a predefined value to calculate the perimeter. This helps focus on the calculation without worrying about input.

#include <iostream>

using namespace std;

int main() {

    double side = 7.0;
    double perimeter = 4 * side;

    cout << "For a square with side " << side 
         << ", the perimeter is: " << perimeter << endl;

    return 0;

}

Using predefined values is useful for testing formulas quickly. Beginners can verify their calculations and understand the logic before introducing dynamic user input.

Program 3: Perimeter Using a Function

This program demonstrates using a function to calculate the perimeter. Functions make code reusable and organized.

#include <iostream>

using namespace std;

double calculatePerimeter(double side) {
    return 4 * side;
}

int main() {

    double side;

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

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

    return 0;

}

The function calculatePerimeter can be called multiple times with different side lengths, demonstrating modular programming. Beginners learn how to separate calculation logic from main program flow, improving readability and maintainability.

Program 4: Perimeter of Multiple Squares Using Arrays

When dealing with multiple squares, arrays allow storing side lengths and calculating perimeters in a loop.

#include <iostream>

using namespace std;

int main() {

    double sides[] {3.0, 5.5, 7.0};
    int n = 3;

    for(int i = 0; i < n; i++) {

        double perimeter = 4 * sides[i];

        cout << "Square " << i+1 << " -> Side: " << sides[i] 
             << ", Perimeter: " << perimeter << endl;

    }

    return 0;

}

This program demonstrates handling multiple datasets efficiently. Loops combined with arrays allow applying the same formula to several items, which is a common pattern in programming tasks.

Frequently Asked Questions (FAQ)

Q1: Can integers be used instead of doubles for side length?
Yes, integers work, but doubles allow fractional side lengths for more precise calculations.

Q2: Why use a function for perimeter calculation?
Functions improve code organization, reusability, and make the program easier to maintain.

Q3: Can the program handle many squares at once?
Yes, arrays or vectors enable calculating perimeters for multiple squares efficiently.

Q4: Is the formula always 4 × side?
Yes, for all squares, the perimeter is four times the side length, regardless of size.

Conclusion

Calculating the perimeter of a square in C++ is a simple yet practical exercise for beginners. By experimenting with user input, predefined values, functions, and arrays, learners strengthen their understanding of arithmetic operations, loops, and modular programming. Practicing these approaches provides a solid foundation for tackling more complex programming challenges.

Additional & References

To continue learning, beginners should practice using functions, arrays, and loops for geometric calculations and multiple squares. Experimenting with these techniques enhances problem-solving and programming efficiency in C++.

Scroll to Top