Convert Radians to Degrees in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to convert an angle from radians to degrees using C programming. You will first learn how to read an angle in radians, then perform the mathematical conversion to obtain the angle in degrees. This lab covers the fundamental trigonometric calculations necessary for various scientific and engineering applications.

The lab consists of two main steps: reading the angle in radians, and multiplying the radian value by 180/PI to convert it to degrees. By the end of this lab, you will have a solid understanding of the radian to degree conversion process in C.


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/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435170{{"`Convert Radians to Degrees in C`"}} c/operators -.-> lab-435170{{"`Convert Radians to Degrees in C`"}} c/user_input -.-> lab-435170{{"`Convert Radians to Degrees in C`"}} c/math_functions -.-> lab-435170{{"`Convert Radians to Degrees in C`"}} end

Read Angle in Radians

In this step, you will learn how to read an angle in radians using C programming. Radians are a standard unit of angular measurement in mathematics and scientific computing.

First, let's create a new C file to implement our radian input:

cd ~/project
nano radian_conversion.c

Now, add the following code to the file:

#include <stdio.h>
#include <math.h>

int main() {
    double radian_angle;

    printf("Enter an angle in radians: ");
    scanf("%lf", &radian_angle);

    printf("Angle in radians: %.2f\n", radian_angle);

    return 0;
}

Compile the program:

gcc radian_conversion.c -o radian_conversion -lm

Run the program and input a radian value:

./radian_conversion

Example output:

Enter an angle in radians: 3.14159
Angle in radians: 3.14

Let's break down the code:

  • We use double to store the radian angle for precise decimal representation
  • scanf() reads the user input as a floating-point number
  • %lf format specifier is used for reading a double-precision floating-point number
  • printf() displays the input angle with two decimal places

Multiply by 180/PI

In this step, you will learn how to convert radians to degrees by multiplying the radian value by 180/PI. This is a standard mathematical conversion formula.

Update the previous C file to include the conversion:

nano ~/project/radian_conversion.c

Modify the code to perform the radian to degree conversion:

#include <stdio.h>
#include <math.h>

int main() {
    double radian_angle, degree_angle;

    printf("Enter an angle in radians: ");
    scanf("%lf", &radian_angle);

    // Convert radians to degrees using 180/PI formula
    degree_angle = radian_angle * (180.0 / M_PI);

    printf("Angle in radians: %.2f\n", radian_angle);
    printf("Angle in degrees: %.2f\n", degree_angle);

    return 0;
}

Compile the updated program:

gcc radian_conversion.c -o radian_conversion -lm

Run the program and input a radian value:

./radian_conversion

Example output:

Enter an angle in radians: 3.14159
Angle in radians: 3.14
Angle in degrees: 180.00

Key points about the conversion:

  • M_PI is a predefined constant in math.h representing π (pi)
  • The conversion formula is: degrees = radians * (180/π)
  • We use 180.0 / M_PI to ensure floating-point division
  • The -lm flag is used to link the math library when compiling

Print the Angle in Degrees

In this final step, you will learn how to format and print the converted angle in degrees with proper formatting and user interaction.

Let's modify the previous C program to enhance the output:

nano ~/project/radian_conversion.c

Update the code with improved formatting and user experience:

#include <stdio.h>
#include <math.h>

int main() {
    double radian_angle, degree_angle;

    printf("Radian to Degree Conversion Program\n");
    printf("-----------------------------------\n");

    printf("Enter an angle in radians: ");
    scanf("%lf", &radian_angle);

    // Convert radians to degrees
    degree_angle = radian_angle * (180.0 / M_PI);

    // Print results with clear formatting
    printf("\nConversion Results:\n");
    printf("Angle in radians: %.4f rad\n", radian_angle);
    printf("Angle in degrees: %.4f °\n", degree_angle);

    return 0;
}

Compile the program:

gcc radian_conversion.c -o radian_conversion -lm

Run the program and test different radian inputs:

./radian_conversion

Example output:

Radian to Degree Conversion Program
-----------------------------------
Enter an angle in radians: 3.14159

Conversion Results:
Angle in radians: 3.1416 rad
Angle in degrees: 180.0000 °

Key improvements in this step:

  • Added a program title and separator
  • Increased decimal precision to 4 places
  • Added unit symbols (rad and °)
  • Improved output formatting for better readability

Summary

In this lab, you learned how to read an angle in radians using C programming, and then convert the radian value to degrees by multiplying it by the formula 180/π. The key steps involved reading the radian angle using the scanf() function, performing the conversion calculation, and then printing the angle in both radians and degrees using the printf() function. The lab demonstrated the fundamental concepts of converting between different angular measurement units, which is an essential skill in various scientific and engineering applications.

Other C Tutorials you may like