Degrees to Radians Conversion¶
Concept Explanation¶
What is it?¶
This program converts an angle measured in degrees to its equivalent in radians. Degrees and
radians are two common units for measuring angles.
- A degree is 1/360th of a full rotation.
- A radian is the angle subtended at the center of a circle by an arc that is equal in length to the radius of the circle. One full rotation is 2π radians.
Why is it important?¶
Radians are the standard unit of angular measure in many areas of mathematics (especially calculus), physics, and engineering. They simplify many formulas and make calculations more natural in mathematical contexts. Understanding this conversion is crucial for working with trigonometric functions and geometric calculations in various programming applications.
Where is it used?¶
- Physics and Engineering: Calculations involving rotational motion, wave mechanics, and oscillations.
- Computer Graphics: Defining rotations and transformations of objects in 2D and 3D environments.
- Mathematics: Trigonometric functions in programming languages usually expect arguments in radians.
- Robotics: Calculating joint angles for robot arms.
Real-world example¶
When programming a game or a simulation where objects rotate, you often define angles in degrees for
user convenience but convert them to radians internally before passing them to mathematical
functions (like sin(), cos()) provided by programming libraries. If a user wants to rotate an
object by 90 degrees, the program converts 90 degrees to π/2 radians before applying the rotation.
Algorithm¶
- Start.
- Get the angle in degrees (
degrees) from the user. - Use the value of
PI(approximately 3.14159...). - Apply the conversion formula:
radians = degrees * PI / 180. - Display the original angle in degrees and the calculated angle in radians.
- End.
Conversion Formula:
radians = degrees × PI / 180
Edge Cases: - Non-numeric input for degrees (handled by language-specific error mechanisms or explicit validation). - Large or negative angle values: The conversion formula holds true for all real numbers.
Implementations¶
import java.util.Scanner;
public class DegreesToRadians {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter angle in degrees: ");
double degrees = scanner.nextDouble();
// Convert degrees to radians
double radians = degrees * Math.PI / 180;
System.out.println("Degrees: " + degrees + "°");
System.out.println("Radians: " + String.format("%.4f", radians) + " rad"); // Format to 4 decimal places
scanner.close();
}
}
#include <stdio.h>
#include <math.h> // Required for M_PI or define PI
int main() {
double degrees, radians;
// M_PI is a common macro for Pi, but might require _USE_MATH_DEFINES on some compilers
// For maximum portability, define PI manually or use acos(-1.0)
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
printf("Enter angle in degrees: ");
scanf("%lf", °rees);
// Convert degrees to radians
radians = degrees * M_PI / 180.0;
printf("%.2lf degrees is equal to %.4lf radians", degrees, radians);
return 0;
}
SET SERVEROUTPUT ON;
DECLARE
degrees_val NUMBER := &Enter_Angle_in_Degrees;
radians_val NUMBER;
PI CONSTANT NUMBER := ACOS(-1); -- Oracle's way to get PI more accurately
BEGIN
DBMS_OUTPUT.PUT_LINE('--- Degrees to Radians Conversion ---');
DBMS_OUTPUT.PUT_LINE('Degrees: ' || degrees_val || '°');
radians_val := degrees_val * PI / 180;
DBMS_OUTPUT.PUT_LINE('Radians: ' || ROUND(radians_val, 4) || ' rad');
END;
/
Explanation¶
- Java: Uses
doublefor angle values.Scannerreads the degrees input.Math.PIprovides the value of Pi. - Python: Reads input as a string and converts to
float. Importsmathmodule formath.pi. - C: Reads
doubleinput usingscanf("%lf", ...)for the degrees. UsesM_PI(often available inmath.h) or a custom definition for Pi.printf("%.4lf", ...)formats output. - Oracle: Implemented in PL/SQL. Uses
NUMBERdata type.ACOS(-1)is a standard way to get the value of Pi. Substitution variable for input.
Complexity Analysis¶
- Time Complexity: O(1) - The number of arithmetic operations is constant.
- Space Complexity: O(1) - A fixed number of variables are used.
Flowchart¶
graph TD
A[Start] --> B[Get Degrees]
B --> C[Set PI value]
C --> D[Calculate Radians = Degrees * PI / 180]
D --> E[Display Degrees and Radians]
E --> F[End]
Sample Dry Run¶
| Step | Degrees | PI | Radians | Description |
|---|---|---|---|---|
| Input | 90.0 | - | - | User enters 90.0 |
| Process | 90.0 | 3.14159 | - | PI is set |
| Process | 90.0 | 3.14159 | 1.5708 | Radians = 90.0 * PI / 180 |
| Output | - | - | 1.5708 | Display "90.00° is 1.5708 rad" |
| End | - | - | - | Program terminates |
Practice Problems¶
Easy¶
- Modify the program to convert radians to degrees.
- Convert angles between degrees, minutes, and seconds to decimal degrees.
Medium¶
- Implement trigonometric functions (sin, cos, tan) without using built-in library functions, which will require understanding radian input.
- Write a program that calculates the distance between two points on a sphere given their latitudes and longitudes (requires spherical trigonometry and radian conversion).
Hard¶
- Develop a simple CAD program that uses radians for internal angle calculations.
"The only source of knowledge is experience." - Albert Einstein