Convert Degrees to Radians in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to convert an angle measurement from degrees to radians using C programming. The lab consists of two main steps: reading the angle in degrees and then performing the conversion to radians by multiplying the angle by π/180. By the end of the lab, you will have a working program that can take an angle input in degrees and output the equivalent value in radians.

The lab provides a step-by-step guide, starting with creating a new C file and prompting the user to enter an angle in degrees. The program then performs the conversion calculation and displays the resulting angle in radians. This lab covers fundamental trigonometric concepts and their implementation in C, which are essential skills for various applications involving angle measurements and calculations.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/constants("`Constants`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435354{{"`Convert Degrees to Radians in C`"}} c/variables -.-> lab-435354{{"`Convert Degrees to Radians in C`"}} c/constants -.-> lab-435354{{"`Convert Degrees to Radians in C`"}} c/operators -.-> lab-435354{{"`Convert Degrees to Radians in C`"}} c/user_input -.-> lab-435354{{"`Convert Degrees to Radians in C`"}} c/math_functions -.-> lab-435354{{"`Convert Degrees to Radians in C`"}} end

Read Angle in Degrees

In this step, you'll learn how to read an angle input in degrees using C programming. We'll create a simple program that allows users to input an angle measurement.

First, let's create a new C file for our degree to radian conversion program:

cd ~/project
nano degree_to_radian.c

Now, enter the following code into the file:

#include <stdio.h>

int main() {
    double degrees;

    // Prompt user to enter an angle in degrees
    printf("Enter an angle in degrees: ");
    scanf("%lf", &degrees);

    // Display the input angle
    printf("You entered: %.2f degrees\n", degrees);

    return 0;
}

Compile and run the program:

gcc degree_to_radian.c -o degree_to_radian
./degree_to_radian

Example output:

Enter an angle in degrees: 45
You entered: 45.00 degrees

Let's break down the code:

  • We use double to store decimal angle values
  • printf() is used to prompt the user for input
  • scanf() reads the user's input into the degrees variable
  • We then print back the entered angle to confirm input

The program demonstrates basic input handling for angle measurements, which is the first step in our degree to radian conversion process.

Multiply by PI/180

In this step, you'll learn how to convert degrees to radians by multiplying the angle by π/180. We'll modify the previous program to perform this conversion.

Open the existing file:

cd ~/project
nano degree_to_radian.c

Update the code to include the conversion calculation:

#include <stdio.h>
#define PI 3.14159265358979323846

int main() {
    double degrees, radians;

    // Prompt user to enter an angle in degrees
    printf("Enter an angle in degrees: ");
    scanf("%lf", &degrees);

    // Convert degrees to radians
    radians = degrees * (PI / 180.0);

    // Display the input angle and converted radians
    printf("Angle in degrees: %.2f\n", degrees);
    printf("Angle in radians: %.4f\n", radians);

    return 0;
}

Compile and run the updated program:

gcc degree_to_radian.c -o degree_to_radian
./degree_to_radian

Example output:

Enter an angle in degrees: 45
Angle in degrees: 45.00
Angle in radians: 0.7854

Key points in the code:

  • We define PI as a constant with a precise value
  • The conversion formula is radians = degrees * (PI / 180.0)
  • We use (PI / 180.0) to convert the angle precisely
  • The result is stored in the radians variable and displayed

This step demonstrates the mathematical conversion from degrees to radians using a simple multiplication formula.

Print the Angle in Radians

In this final step, you'll learn how to format and print the converted angle in radians with proper precision and formatting.

Open the existing file:

cd ~/project
nano degree_to_radian.c

Update the code to improve output formatting:

#include <stdio.h>
#define PI 3.14159265358979323846

int main() {
    double degrees, radians;

    // Prompt user to enter an angle in degrees
    printf("Degree to Radian Converter\n");
    printf("-------------------------\n");
    printf("Enter an angle in degrees: ");
    scanf("%lf", &degrees);

    // Convert degrees to radians
    radians = degrees * (PI / 180.0);

    // Print formatted output
    printf("\nConversion Results:\n");
    printf("Angle in degrees: %.2f°\n", degrees);
    printf("Angle in radians: %.4f rad\n", radians);

    return 0;
}

Compile and run the final program:

gcc degree_to_radian.c -o degree_to_radian
./degree_to_radian

Example output:

Degree to Radian Converter
-------------------------
Enter an angle in degrees: 90

Conversion Results:
Angle in degrees: 90.00°
Angle in radians: 1.5708 rad

Key improvements in this version:

  • Added a title and separator for better user experience
  • Included degree (°) and radian (rad) units in the output
  • Formatted the output with clear sections
  • Used precise decimal formatting for both degrees and radians

This final step completes the degree to radian conversion program, providing a user-friendly interface for trigonometric calculations.

Summary

In this lab, you first learned how to read an angle input in degrees using C programming. You created a simple program that prompts the user to enter an angle measurement and displays the input value. Then, you modified the program to convert the angle from degrees to radians by multiplying the input by π/180. The converted angle in radians is then displayed to the user. Overall, this lab covers the fundamental steps required to convert degrees to radians in C programming.

Other C Tutorials you may like