C++ Program to Convert Degrees to Radians

C++ Program to Convert Degrees to Radians

Angles can be represented in degrees or radians, and converting between the two is essential in programming, especially when dealing with trigonometric functions. Many C++ math functions, like sin() and cos(), expect angles in radians, so knowing how to convert degrees to radians is a practical skill. Learning this in C++ helps beginners understand mathematical operations, constants, loops, and functions, which are valuable for solving real-world problems.

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: Convert Degrees to Radians Using User Input

This program allows the user to input an angle in degrees and converts it to radians using the formula: radians = degrees × (π / 180).

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double degrees, radians;

    cout << "Enter angle in degrees: " << endl;
    cin >> degrees;

    radians = degrees * (M_PI / 180);

    cout << degrees << " degrees = " << radians << " radians" << endl;

    return 0;

}

The program reads a double value for the angle in degrees. It then multiplies this value by π / 180 to convert it to radians. Beginners can learn how constants, arithmetic, and output work together to perform conversions.

Program 2: Convert Predefined Degrees to Radians

Sometimes you want to convert a known degree value without user input. This program shows how to do that.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double degrees = 90; // 90 degrees
    double radians = degrees * (M_PI / 180);

    cout << degrees << " degrees = " << radians << " radians" << endl;

    return 0;

}

Using a predefined value is useful for testing and understanding the conversion. Beginners can experiment by changing the degrees variable and instantly see the corresponding radian value.

Program 3: Convert Multiple Degrees Using an Array

When working with multiple angles, arrays can help convert all values efficiently. This program demonstrates converting multiple degree values to radians.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double degrees[] {0, 30, 45, 60, 90};
    int n = 5;

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

        double radians = degrees[i] * (M_PI / 180);

        cout << degrees[i] << " degrees = " << radians << " radians" << endl;

    }

    return 0;

}

By using an array and a loop, this program efficiently converts multiple angles. Beginners see how to iterate through data and apply formulas, which is a valuable skill for real-world programming.

Program 4: Convert Degrees to Radians Using a Function

Encapsulating the conversion in a function makes your code cleaner and reusable. This program demonstrates that approach.

#include <iostream>
#include <cmath>

using namespace std;

double degreesToRadians(double deg) {
    return deg * (M_PI / 180);
}

int main() {

    double degrees;

    cout << "Enter angle in degrees: " << endl;
    cin >> degrees;

    double radians = degreesToRadians(degrees);

    cout << degrees << " degrees = " << radians << " radians" << endl;

    return 0;

}

Using a function makes the conversion reusable and modular. Beginners can easily apply this function to multiple values or integrate it into larger programs, learning the importance of code organization and readability.

Frequently Asked Questions (FAQ)

Q1: Why multiply by π/180 to convert degrees to radians?
A full circle is 360 degrees, which equals 2π radians. Dividing π by 180 gives the conversion factor.

Q2: Can I use float instead of double?
Yes, but double is more precise, which is important for accurate calculations.

Q3: How can I convert multiple degree values at once?
You can store the degree values in an array or vector and use loops or functions to convert each value.

Q4: Are there built-in functions for this in C++?
C++ doesn’t provide a direct function, but you can use your own function or math libraries that handle angles.

Conclusion

Converting degrees to radians in C++ is a straightforward yet essential skill. Beginners can start with single-value conversions, then move on to arrays and functions for multiple values. Practicing these methods helps improve understanding of arithmetic operations, constants, loops, and modular programming, making it a valuable foundation for trigonometric and mathematical programming.

Additional & References

To continue learning, beginners should practice using functions, arrays, and constants for mathematical calculations and angle conversions. Experimenting with multiple degree values and applying the concepts in real-world problems strengthens both coding and mathematical understanding.

Scroll to Top