C++ Program to Find Arcsine - Arccosine and Arctangent of an Angle

C++ Program to Find Arcsine – Arccosine and Arctangent of an Angle

Trigonometry is a cornerstone of mathematics, and understanding inverse trigonometric functions is crucial for fields like physics, engineering, and computer graphics. Functions like arcsine (asin), arccosine (acos), and arctangent (atan) allow us to determine the angle corresponding to a specific sine, cosine, or tangent value. Learning how to compute these in C++ not only strengthens your grasp of trigonometry but also introduces you to angle conversions, mathematical libraries, and error handling in programming.

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: Compute Arcsine, Arccosine, and Arctangent from User Input

This program allows the user to enter a value, and it calculates the corresponding angle in degrees for arcsine, arccosine, and arctangent. It ensures that the input is within the valid range for each function.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double value;

    cout << "Enter a value between -1 and 1 for arcsine and arccosine: " << endl;
    cin >> value;

    if(value >= -1 && value <= 1) {

        double angleAsin = asin(value) * 180 / M_PI;
        double angleAcos = acos(value) * 180 / M_PI;

        cout << "Arcsine (asin) in degrees: " << angleAsin << endl;
        cout << "Arccosine (acos) in degrees: " << angleAcos << endl;

    } else {

        cout << "Value out of range for arcsine and arccosine." << endl;

    }

    double valueAtan;

    cout << "Enter a value for arctangent: " << endl;
    cin >> valueAtan;

    double angleAtan = atan(valueAtan) * 180 / M_PI;

    cout << "Arctangent (atan) in degrees: " << angleAtan << endl;

    return 0;

}

This program demonstrates the use of asin, acos, and atan functions from the C++ cmath library. Converting radians to degrees makes the results more understandable for beginners. Checking the input range prevents errors, helping learners understand the importance of domain constraints in inverse trigonometry.

Program 2: Predefined Values for Quick Calculation

To quickly verify calculations or test common values, predefined inputs can be used. This program computes arcsine, arccosine, and arctangent for fixed values.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double values[] {0.5, 0.7071, 1.0};

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

        double val = values[i];
        double asinDeg = asin(val) * 180 / M_PI;
        double acosDeg = acos(val) * 180 / M_PI;
        double atanDeg = atan(val) * 180 / M_PI;

        cout << "Value: " << val << endl;
        cout << "Arcsine: " << asinDeg << "°, "
             << "Arccosine: " << acosDeg << "°, "
             << "Arctangent: " << atanDeg << "°" << endl << endl;

    }

    return 0;

}

Using predefined values allows beginners to verify their understanding of standard angles like 30°, 45°, and 90° in trigonometry. It also reinforces the concept of automating calculations using arrays and loops.

Program 3: Function-Based Approach for Multiple Angles

Modular programming helps organize code, especially when computing multiple inverse trigonometric values for different inputs.

#include <iostream>
#include <cmath>

using namespace std;

double toDegrees(double radians) {
    return radians * 180 / M_PI;
}

void computeInverseTrig(double val) {

    if(val >= -1 && val <= 1)
        cout << "asin: " << toDegrees(asin(val)) << "°, acos: " << toDegrees(acos(val)) << "°" << endl;

    else
        cout << "asin and acos undefined for value: " << val << endl;

    cout << "atan: " << toDegrees(atan(val)) << "°" << endl;

}

int main() {

    double inputs[] {0.5, 0.866, 2.0, -0.5};
    int n = 4;

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

        cout << "Input: " << inputs[i] << endl;
        computeInverseTrig(inputs[i]);
        cout << endl;

    }

    return 0;

}

This approach separates calculation logic from input/output, making it easier to reuse the function for multiple values. Beginners also learn how to handle domain errors and practice modular programming.

Frequently Asked Questions (FAQ)

Q1: What is the valid input range for arcsine and arccosine?
The input must be between -1 and 1; otherwise, the functions are undefined.

Q2: Can arctangent accept any real number?
Yes, atan can accept any real value.

Q3: Why do we convert radians to degrees?
Radians are the default output of cmath functions, but degrees are more intuitive for most practical applications.

Q4: Are there differences in function behavior for negative inputs?
Yes. For negative values, arcsine and arctangent return negative angles, while arccosine returns an angle between 0° and 180°.

Conclusion

Calculating arcsine, arccosine, and arctangent in C++ helps beginners understand inverse trigonometric functions, angle conversions, input validation, and modular coding. By experimenting with user input, predefined values, and functions, learners can build confidence in handling both mathematical and programming challenges. Mastery of these concepts is essential for applications in geometry, physics, engineering, and computer graphics.

Additional & References

Beginners are encouraged to practice with different input values, including negative numbers and decimals, to strengthen their understanding of inverse trigonometric functions. Using functions and arrays enhances both reusability and efficiency in code.

Scroll to Top