Posted on by Kalkicode
Code Conversion

Convert degree to radian

The problem is to convert a given angle from degrees to radians. In trigonometry and mathematics, angles can be measured in two units: degrees and radians. Degrees are commonly used in everyday life, while radians are often used in advanced mathematical calculations and trigonometric functions.

radians = (degrees * pi) / 180

Explanation using Example

Let's consider the test cases from the provided code:

  1. Case 1: Degree = 1 To convert 1 degree to radians, we use the formula radians = degree * (π / 180). Substituting the given value, we get radians = 1 * (π / 180) = 0.017453. Therefore, 1 degree is equal to approximately 0.017453 radians.

  2. Test Case 2: Degree = 7.8 To convert 7.8 degrees to radians, we use the formula radians = degree * (π / 180). Substituting the given value, we get radians = 7.8 * (π / 180) = 0.136136. Therefore, 7.8 degrees is equal to approximately 0.136136 radians.

  3. Test Case 3: Degree = 167.5 To convert 167.5 degrees to radians, we use the formula radians = degree * (π / 180). Substituting the given value, we get radians = 167.5 * (π / 180) = 2.923426. Therefore, 167.5 degrees is equal to approximately 2.923426 radians.

Pseudocode

degree_to_radian(degree)
    // Convert given degree to radian
    radians = degree * (π / 180)
    print "Degree : degree Radian : radians"

Algorithm Explanation

The degree_to_radian function takes a degree value as input and converts it to its equivalent radian representation using the formula radians = degree * (π / 180). The formula uses the mathematical constant π (pi), which represents the ratio of the circumference of a circle to its diameter and is approximately equal to 3.14159.

In the main function, the test cases are provided, and the degree_to_radian function is called for each test case. The function correctly calculates the radian value for each input degree and prints both the original degree and its equivalent radian.

Program Solution

//C Program 
//Convert degree to radian
#include <stdio.h>
#include <math.h>

//Find the radian of given degree
void degree_to_radian(double degree)
{
  // Formula : degree * (π / 180);
  // Convert given degree to radian
  double radians = degree * (M_PI / 180);
  printf("Degree : %lf Radian : %lf\n", degree, radians);
}
int main()
{
  //Simple test
  degree_to_radian(1);
  degree_to_radian(7.8);
  degree_to_radian(167.5);
  return 0;
}

Output

Degree : 1.000000 Radian : 0.017453
Degree : 7.800000 Radian : 0.136136
Degree : 167.500000 Radian : 2.923426
/*
  Java program
  Convert degree to radian
*/
class MyMath
{
  //Find the radian of given degree
  public void degree_to_radian(double degree)
  {
    // Convert given degree to radian
    // Formula : degree * (π / 180);
    double radians = degree * (Math.PI / 180);
    //Display result
    System.out.print("Degree : " + degree + " Radian : " + radians + "\n");
  }
  public static void main(String[] args)
  {
    MyMath obj = new MyMath();
    //Simple test
    obj.degree_to_radian(1);
    obj.degree_to_radian(7.8);
    obj.degree_to_radian(167.5);
  }
}

Output

Degree : 1.0 Radian : 0.017453292519943295
Degree : 7.8 Radian : 0.1361356816555577
Degree : 167.5 Radian : 2.923426497090502
/*
  C++ program
  Convert degree to radian
*/
#include<iostream>
#include<math.h>

using namespace std;
class MyMath
{
  public:
    //Find the radian of given degree
    void degree_to_radian(double degree)
    {
      // Convert given degree to radian
      // Formula : degree * (π / 180);
      double radians = degree * (M_PI / 180);
      //Display result
      cout << "Degree : " << degree << " Radian : " << radians << "\n";
    }
};
int main()
{
  MyMath obj =  MyMath();
  //Simple test
  obj.degree_to_radian(1);
  obj.degree_to_radian(7.8);
  obj.degree_to_radian(167.5);
  return 0;
}

Output

Degree : 1 Radian : 0.0174533
Degree : 7.8 Radian : 0.136136
Degree : 167.5 Radian : 2.92343
/*
  C# program
  Convert degree to radian
*/
using System;
class MyMath
{
  //Find the radian of given degree
  public void degree_to_radian(double degree)
  {
    // Convert given degree to radian
    // Formula : degree * (π / 180);
    double radians = degree * (Math.PI / 180);
    //Display result
    Console.Write("Degree : " + degree + " Radian : " + radians + "\n");
  }
  public static void Main(String[] args)
  {
    MyMath obj = new MyMath();
    //Simple test
    obj.degree_to_radian(1);
    obj.degree_to_radian(7.8);
    obj.degree_to_radian(167.5);
  }
}

Output

Degree : 1 Radian : 0.0174532925199433
Degree : 7.8 Radian : 0.136135681655558
Degree : 167.5 Radian : 2.9234264970905
<?php
/*
  Php program
  Convert degree to radian
*/
class MyMath
{
  //Find the radian of given degree
  public  function degree_to_radian($degree)
  {
    // Convert given degree to radian
    // Formula : degree * (π / 180);
    $radians = $degree * (M_PI / 180);
    echo "Degree : ". $degree ." Radian : ". $radians ."\n";
  }
}

function main()
{
  $obj = new MyMath();
  //Simple test
  $obj->degree_to_radian(1);
  $obj->degree_to_radian(7.8);
  $obj->degree_to_radian(167.5);
}
main();

Output

Degree : 1 Radian : 0.017453292519943
Degree : 7.8 Radian : 0.13613568165556
Degree : 167.5 Radian : 2.9234264970905
/*
  Node Js program
  Convert degree to radian
*/
class MyMath
{
  //Find the radian of given degree
  degree_to_radian(degree)
  {
    // Convert given degree to radian
    // Formula : degree * (π / 180);
    var radians = degree * (Math.PI / 180);
    process.stdout.write("Degree : " + degree + " Radian : " + radians + "\n");
  }
}

function main()
{
  var obj = new MyMath();
  //Simple test
  obj.degree_to_radian(1);
  obj.degree_to_radian(7.8);
  obj.degree_to_radian(167.5);
}
main();

Output

Degree : 1 Radian : 0.017453292519943295
Degree : 7.8 Radian : 0.1361356816555577
Degree : 167.5 Radian : 2.923426497090502
#   Python 3 program
#   Convert degree to radian
import math

class MyMath :
  # Find the radian of given degree
  def degree_to_radian(self, degree) :
    #  Convert given degree to radian
    #  Formula : degree * (π / 180)
    radians = degree * (math.pi / 180)
    print("Degree : ", degree ," Radian : ", radians ,"\n", end = "")
  

def main() :
  obj = MyMath()
  # Simple test
  obj.degree_to_radian(1)
  obj.degree_to_radian(7.8)
  obj.degree_to_radian(167.5)

if __name__ == "__main__": main()

Output

Degree :  1  Radian :  0.017453292519943295
Degree :  7.8  Radian :  0.1361356816555577
Degree :  167.5  Radian :  2.923426497090502
#   Ruby program
#   Convert degree to radian

class MyMath

  # Find the radian of given degree
  def degree_to_radian(degree)
  
    #  Convert given degree to radian
    #  Formula : degree * (π / 180)
    radians = degree * (Math::PI / 180)
    # Display result
    print("Degree : ", degree ," Radian : ", radians ,"\n")
  end
end
def main()

  obj = MyMath.new()
  # Simple test
  obj.degree_to_radian(1)
  obj.degree_to_radian(7.8)
  obj.degree_to_radian(167.5)
end
main()

Output

Degree : 1 Radian : 0.017453292519943295
Degree : 7.8 Radian : 0.1361356816555577
Degree : 167.5 Radian : 2.923426497090502
/*
  Scala program
  Convert degree to radian
*/
class MyMath
{
  //Find the radian of given degree
  def degree_to_radian(degree: Double): Unit = {
    // Convert given degree to radian
    // Formula : degree * (π / 180);
    var radians: Double = degree * (Math.PI / 180);
    //Display result
    print("Degree : " + degree + " Radian : " + radians + "\n");
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyMath = new MyMath();
    //Simple test
    obj.degree_to_radian(1);
    obj.degree_to_radian(7.8);
    obj.degree_to_radian(167.5);
  }
}

Output

Degree : 1.0 Radian : 0.017453292519943295
Degree : 7.8 Radian : 0.1361356816555577
Degree : 167.5 Radian : 2.923426497090502
/*
  Swift program
  Convert degree to radian
*/
import Foundation
class MyMath
{
  //Find the radian of given degree
  func degree_to_radian(_ degree: Double)
  {
    // Convert given degree to radian
    // Formula : degree * (π / 180);
    let radians: Double = Double(degree) * (Double.pi / 180);
    print("Degree : ", degree ," Radian : ", radians ,"\n", terminator: "");
  }
}
func main()
{
  let obj: MyMath = MyMath();
  //Simple test
  obj.degree_to_radian(1);
  obj.degree_to_radian(7.8);
  obj.degree_to_radian(167.5);
}
main();

Output

Degree :  1.0  Radian :  0.0174532925199433
Degree :  7.8  Radian :  0.136135681655558
Degree :  167.5  Radian :  2.9234264970905

Resultant Output Explanation

The code correctly converts the given degree values to their equivalent radian representations for the provided test cases:

  1. For 1 degree, the equivalent radian representation is approximately 0.017453.
  2. For 7.8 degrees, the equivalent radian representation is approximately 0.136136.
  3. For 167.5 degrees, the equivalent radian representation is approximately 2.923426.

The algorithm performs the degree to radian conversion accurately, and the results match the expected values. The code is simple, efficient, and provides the correct radian representation for any given degree angle.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment