C++ Program to Find Perimeter of a Circle

C++ Program to Find Perimeter of a Circle

Calculating the perimeter of a circle, also known as the circumference, is one of the fundamental concepts in geometry. The formula for the perimeter is C = 2 × π × r, where r is the radius of the circle. Learning how to compute this in C++ helps beginners practice arithmetic operations, constants, user input, functions, and reusable code, making it a great way to strengthen foundational programming skills with real-world applications.

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 of a Circle Using User Input

This program asks the user to enter the radius and calculates the perimeter using the formula. It’s a simple way to practice input/output and basic arithmetic in C++.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radius, perimeter;

    cout << "Enter the radius of the circle: " << endl;
    cin >> radius;

    perimeter = 2 * M_PI * radius;
    cout << "The perimeter of the circle is: " << perimeter << endl;

    return 0;

}

Here, the program multiplies the radius by 2 and π to compute the perimeter. Beginners can understand this as a straightforward way to apply constants and multiplication for geometric calculations.

Program 2: Perimeter Using Predefined Radius

Sometimes, the radius is known beforehand. This program demonstrates calculating the perimeter using a predefined value, which helps beginners focus on the formula without requiring input.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radius = 7.0;  // predefined radius
    double perimeter = 2 * M_PI * radius;

    cout << "For a circle with radius " << radius << ", the perimeter is: " << perimeter << endl;

    return 0;

}

This approach allows beginners to verify calculations quickly and focus on understanding the formula without handling input.

Program 3: Perimeter Using a Function

Using a function makes the program modular and reusable. This program defines a function to calculate the perimeter, separating calculation logic from the main program.

#include <iostream>
#include <cmath>

using namespace std;

double calculatePerimeter(double r) {
    return 2 * M_PI * r;
}

int main() {

    double radius;

    cout << "Enter the radius of the circle: " << endl;
    cin >> radius;

    double perimeter = calculatePerimeter(radius);

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

    return 0;

}

This version helps beginners learn modular programming. The function can be reused to calculate perimeters for multiple circles without rewriting the formula.

Program 4: Perimeter of Multiple Circles Using Vectors

Vectors allow storing multiple datasets efficiently. This program calculates the perimeter for several circles stored in a vector, demonstrating dynamic data handling.

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

double calculatePerimeter(double r) {
    return 2 * M_PI * r;
}

int main() {

    vector<double> radii = {3.0, 5.0, 7.5};

    cout << "Calculating perimeters for multiple circles:" << endl;

    for(double r : radii) {

        double perimeter = calculatePerimeter(r);
        cout << "Radius: " << r << " -> Perimeter: " << perimeter << endl;

    }

    return 0;

}

This program introduces beginners to vectors, showing how to handle multiple inputs efficiently while applying the same formula repeatedly.

Frequently Asked Questions (FAQ)

Q1: Can I use integers instead of double for the radius?
Yes, but using double provides more accurate results, especially for fractional radii.

Q2: Why define PI instead of using M_PI from <cmath>?
Defining PI is simple for beginners. M_PI can be used if you include <cmath>.

Q3: Can this program handle multiple circles?
Yes, using vectors (as in Program 4) allows calculating perimeters for several circles at once.

Q4: Is there a difference between perimeter and circumference?
No, both terms refer to the same measurement around the circle.

Conclusion

Calculating the perimeter of a circle in C++ is an excellent way for beginners to practice arithmetic operations, constants, functions, and vectors. By experimenting with user input, predefined values, functions, and dynamic arrays, learners gain practical experience in modular programming and data handling. Understanding these approaches not only strengthens coding skills but also shows how programming applies to real-world geometric problems.

Additional & References

To continue learning, beginners should explore functions, vectors, and constants for geometric calculations. Practicing with multiple circles will strengthen both programming and problem-solving skills.

Scroll to Top