Trigonometric functions are widely used in mathematics, physics, and engineering. While sine, cosine, and tangent are commonly applied, cosecant (csc), secant (sec), and cotangent (cot) are equally important for solving advanced problems in wave mechanics, circuits, and geometry. Learning how to calculate these in C++ helps beginners understand inverse operations, angle conversions, and safe handling of undefined values, making it a practical exercise for developing programming skills.

with hands-on learning.
get the skills and confidence to land your next move.
Program 1: Calculate Cosecant, Secant, and Cotangent Using User Input in Degrees
This program allows the user to input an angle in degrees and calculates its cosecant, secant, and cotangent values. It also handles cases where these functions are undefined.
#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;
double sinVal = sin(angleRadians);
double cosVal = cos(angleRadians);
if(sinVal != 0)
cout << "Cosecant (csc): " << 1/sinVal << endl;
else
cout << "Cosecant (csc) is undefined for this angle." << endl;
if(cosVal != 0)
cout << "Secant (sec): " << 1/cosVal << endl;
else
cout << "Secant (sec) is undefined for this angle." << endl;
if(tan(angleRadians) != 0)
cout << "Cotangent (cot): " << 1/tan(angleRadians) << endl;
else
cout << "Cotangent (cot) is undefined for this angle." << endl;
return 0;
}
This program first converts degrees to radians, then calculates sine, cosine, and tangent. Using these values, it computes csc, sec, and cot as the reciprocal. Beginners learn how to check for undefined values, which is essential for avoiding runtime errors.
Program 2: Predefined Angle for Quick Calculation
Sometimes, it is useful to calculate trigonometric functions for common angles like 30°, 45°, or 60°. This program demonstrates using a fixed angle to quickly compute cosecant, secant, and cotangent.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double angleDegrees = 45.0;
double angleRadians = angleDegrees * M_PI / 180.0;
double sinVal = sin(angleRadians);
double cosVal = cos(angleRadians);
cout << "Angle: " << angleDegrees << " degrees" << endl;
cout << "Cosecant (csc): " << (sinVal != 0 ? 1/sinVal : NAN) << endl;
cout << "Secant (sec): " << (cosVal != 0 ? 1/cosVal : NAN) << endl;
cout << "Cotangent (cot): " << (tan(angleRadians) != 0 ? 1/tan(angleRadians) : NAN) << endl;
return 0;
}
Using predefined angles allows beginners to quickly verify results and understand how reciprocal trigonometric functions behave. It’s particularly useful when testing known values.
Program 3: Using Functions for Reusable Calculations
Encapsulating the calculations inside functions makes the program modular and reusable, allowing easy computation for multiple angles.
#include <iostream>
#include <cmath>
using namespace std;
double toRadians(double degrees) {
return degrees * M_PI / 180.0;
}
void printReciprocalTrig(double degrees) {
double radians = toRadians(degrees);
double sinVal = sin(radians);
double cosVal = cos(radians);
double tanVal = tan(radians);
cout << "Angle: " << degrees << " degrees" << endl;
if(sinVal != 0)
cout << "Cosecant (csc): " << 1/sinVal << endl;
else
cout << "Cosecant (csc) is undefined." << endl;
if(cosVal != 0)
cout << "Secant (sec): " << 1/cosVal << endl;
else
cout << "Secant (sec) is undefined." << endl;
if(tanVal != 0)
cout << "Cotangent (cot): " << 1/tanVal << endl;
else
cout << "Cotangent (cot) is undefined." << endl;
}
int main() {
double angle;
cout << "Enter an angle in degrees: " << endl;
cin >> angle;
printReciprocalTrig(angle);
return 0;
}
This approach separates logic from input/output, improving readability and making it easier to reuse the function for multiple angles or in larger programs.
Program 4: Compute for Multiple Angles Using Arrays
For analyzing multiple angles at once, arrays and loops can automate calculations efficiently.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double angles[] {30, 45, 60, 90};
int n = 4;
for(int i = 0; i < n; i++) {
double radians = angles[i] * M_PI / 180.0;
double sinVal = sin(radians);
double cosVal = cos(radians);
double tanVal = tan(radians);
cout << "Angle: " << angles[i] << " degrees" << endl;
cout << "Cosecant (csc): " << (sinVal != 0 ? 1/sinVal : NAN) << ", ";
cout << "Secant (sec): " << (cosVal != 0 ? 1/cosVal : NAN) << ", ";
cout << "Cotangent (cot): " << (tanVal != 0 ? 1/tanVal : NAN) << endl;
}
return 0;
}
This program highlights efficiency and automation, showing beginners how loops and arrays reduce repetitive code while performing calculations for multiple angles.
Frequently Asked Questions (FAQ)
Q1: Why do we use 1/sin, 1/cos, and 1/tan for csc, sec, and cot?
These functions are defined as the reciprocal of sine, cosine, and tangent, respectively.
Q2: Are there angles where these functions are undefined?
Yes. Cosecant is undefined at 0°, secant at 90°, and cotangent at 0° or 180°.
Q3: Can we use floating-point angles?
Yes, double
or float
types allow precise computation for decimal angles.
Q4: What is the difference between tangent and cotangent?
Tangent is the ratio of sine to cosine, while cotangent is the reciprocal of tangent (cosine/sine).
Conclusion
Calculating cosecant, secant, and cotangent in C++ is an excellent exercise to learn reciprocal trigonometric functions, angle conversion, loops, arrays, and functions. Starting with user input and predefined angles, then moving to functions and arrays, helps beginners understand both mathematical concepts and programming practices. Regular practice strengthens problem-solving skills and prepares learners for applications in engineering, physics, and computer graphics.
Additional & References
Beginners are encouraged to practice with different angles, including negative and decimal angles, to fully understand reciprocal trigonometric functions. Experimenting with loops and functions enhances both efficiency and code organization.
- C++ cmath Library – Official documentation for trigonometric and mathematical functions.
- GeeksforGeeks: Reciprocal Trigonometric Functions – Tutorial on csc, sec, and cot.
- Programiz C++ Tutorials – Beginner-friendly guide to loops, arrays, and functions.
- Math is Fun: Trigonometry Basics – Explanation of sine, cosine, and tangent for practical understanding.
- LearnCpp: Functions and Arrays – Guides on modular coding and handling multiple data efficiently.