C++ Program to Find Sine - Cosine and Tangent of an Angle

C++ Program to Find Sine – Cosine and Tangent of an Angle

Trigonometry plays a vital role in fields such as physics, engineering, and computer graphics. Calculating sine, cosine, and tangent of an angle allows us to analyze waveforms, rotations, and geometric relationships. Learning to compute these values in C++ introduces beginners to the cmath library, mathematical functions, and angle conversions, making it a practical and engaging programming exercise.

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: Calculate Sine, Cosine, and Tangent Using User Input in Degrees

This program asks the user to input an angle in degrees and calculates its sine, cosine, and tangent. It demonstrates how to handle angle conversion from degrees to radians, which is required by C++ trigonometric functions.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double angleDegrees;

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

    double angleRadians = angleDegrees * M_PI / 180.0;

    cout << "Sine: " << sin(angleRadians) << endl;
    cout << "Cosine: " << cos(angleRadians) << endl;
    cout << "Tangent: " << tan(angleRadians) << endl;

    return 0;

}

In this program, the angle is converted from degrees to radians using the formula radians = degrees * π / 180, since C++’s sin(), cos(), and tan() functions expect radians. Beginners can see how input, arithmetic operations, and math functions work together.

Program 2: Calculate Trigonometric Values Using a Predefined Angle

For quick calculations, sometimes a predefined angle can be used. This program demonstrates using a fixed angle to compute sine, cosine, and tangent.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double angleDegrees = 45.0;

    double angleRadians = angleDegrees * M_PI / 180.0;

    cout << "Angle: " << angleDegrees << " degrees" << endl;
    cout << "Sine: " << sin(angleRadians) << endl;
    cout << "Cosine: " << cos(angleRadians) << endl;
    cout << "Tangent: " << tan(angleRadians) << endl;

    return 0;

}

Using predefined values allows beginners to test the program quickly and observe expected outputs for standard angles like 30°, 45°, or 60°. It reinforces understanding of trigonometric functions in C++.

Program 3: Using Functions to Calculate Sine, Cosine, and Tangent

Encapsulating trigonometric calculations inside functions makes code reusable and organized, which is especially helpful when computing these values for multiple angles.

#include <iostream>
#include <cmath>

using namespace std;

double toRadians(double degrees) {
    return degrees * M_PI / 180.0;
}

void printTrigonometry(double degrees) {

    double radians = toRadians(degrees);

    cout << "Angle: " << degrees << " degrees" << endl;
    cout << "Sine: " << sin(radians) << endl;
    cout << "Cosine: " << cos(radians) << endl;
    cout << "Tangent: " << tan(radians) << endl;

}

int main() {

    double angle;

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

    printTrigonometry(angle);

    return 0;

}

Functions help beginners separate logic from input/output, making the program cleaner and easier to maintain. This modular approach is especially useful in larger programs or when working with multiple angles.

Program 4: Calculate Trigonometric Values for Multiple Angles Using Arrays

When multiple angles need analysis, arrays and loops can automate the computation of sine, cosine, and tangent for several values.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

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

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

        double radians = angles[i] * M_PI / 180.0;

        cout << "Angle: " << angles[i] << " degrees" << endl;
        cout << "Sine: " << sin(radians) << ", ";
        cout << "Cosine: " << cos(radians) << ", ";
        cout << "Tangent: " << tan(radians) << endl;

    }

    return 0;

}

Using arrays and loops demonstrates how to handle multiple values efficiently and reduces repetitive code. Beginners can apply this technique in applications like physics simulations, graphics, and engineering calculations.

Frequently Asked Questions (FAQ)

Q1: Why convert degrees to radians?
C++ trigonometric functions (sin(), cos(), tan()) require angles in radians, not degrees.

Q2: Can I input negative angles?
Yes, negative angles are allowed and follow the standard trigonometric conventions.

Q3: What happens at 90° or 270° for tangent?
Tangent is undefined at 90° and 270°; in C++, this may result in very large values or infinity.

Q4: Can these functions handle floating-point angles?
Yes, double or float types can store angles with decimal points for precise calculations.

Conclusion

Calculating sine, cosine, and tangent in C++ is an excellent way for beginners to learn mathematical functions, input handling, loops, arrays, and functions. Starting with user input and predefined values, then moving to functions and arrays, helps learners understand both trigonometry and C++ programming. Practicing these programs strengthens problem-solving skills and prepares beginners for more complex applications in science, engineering, and graphics.

Additional & References

To continue learning, beginners should practice trigonometric functions with different angles, radians, and arrays. Experimenting with loops and functions improves coding efficiency and mathematical understanding.

Scroll to Top