Posted on by Kalkicode
Code Conversion

Convert gradian to degree

In geometry and trigonometry, angles are often measured in degrees. However, there are other angular measurement systems as well, such as radians and gradians. Gradians, also known as gon or grad, is an angular measurement system where a right angle is divided into 100 gradians, much like how a degree is divided into 60 minutes. Converting between different angular units is a common task, especially when working with various mathematical and scientific applications.

Problem Statement and Description

The problem at hand is to convert an angle given in gradians to its equivalent value in degrees. The formula to convert from gradians to degrees is (gradian * 180) / 200. This formula takes the given angle in gradians, multiplies it by 180, and then divides by 200 to obtain the equivalent angle in degrees.

Example

Let's take an example to understand the problem better. Suppose we have an angle of 50 gradians that we want to convert to degrees. Using the formula:

Degree = (Gradian * 180) / 200
    Degree = (50 * 180) / 200
    Degree = 45

So, 50 gradians is equivalent to 45 degrees.

Idea to Solve the Problem

To solve this problem, we need to implement a program that takes a value in gradians as input, applies the conversion formula, and then outputs the equivalent angle in degrees. We will use a function to perform this conversion, and the main program will call this function for different test cases.

Pseudocode

Here's the pseudocode for the program:

function gradian_to_degree(gradian):
    degree = (gradian * 180) / 200
    return degree

Algorithm Explanation

  1. Define the gradian_to_degree function that takes a parameter gradian.
  2. Inside the function, calculate the equivalent degree using the formula (gradian * 180) / 200.
  3. Return the calculated degree.
  4. In the main program, call the gradian_to_degree function with different test cases: 1, 70.8, and 1000.5.
  5. Print the results.

Code Solution

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

#include <math.h>

//Find the degree of given gradian
void gradian_to_degree(double gradian)
{
	// Formula : (gradian*180)/200
	// Calculate given gradian to degree 
	double degree = (gradian * 180) / 200;
	//Display result
	printf("Gradian : %lf  Degree : %lf\n", gradian, degree);
}
int main()
{
	//Simple test
	gradian_to_degree(1);
	gradian_to_degree(70.8);
	gradian_to_degree(1000.5);
	return 0;
}

Output

Gradian : 1.000000  Degree : 0.900000
Gradian : 70.800000  Degree : 63.720000
Gradian : 1000.500000  Degree : 900.450000
/*
  Java program
  Convert gradian to degree 
*/
class MyMath
{
	//Find the degree of given gradian
	public void gradian_to_degree(double gradian)
	{
		// Formula : (gradian*180)/200
		// Calculate given gradian to degree 
		double degree = (gradian * 180) / 200;
		System.out.print("Gradian : " + gradian + " Degree : " + degree + "\n");
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		//Simple test
		obj.gradian_to_degree(1);
		obj.gradian_to_degree(70.80);
		obj.gradian_to_degree(1000.50);
	}
}

Output

Gradian : 1.0 Degree : 0.9
Gradian : 70.8 Degree : 63.72
Gradian : 1000.5 Degree : 900.45
/*
  C++ program
  Convert gradian to degree 
*/
#include<iostream>

using namespace std;
class MyMath
{
	public:
		//Find the degree of given gradian
		void gradian_to_degree(double gradian)
		{
			// Formula : (gradian*180)/200
			// Calculate given gradian to degree 
			double degree = (gradian * 180) / 200;
			cout << "Gradian : " << gradian << " Degree : " << degree << "\n";
		}
};
int main()
{
	MyMath obj = MyMath();
	//Simple test
	obj.gradian_to_degree(1);
	obj.gradian_to_degree(70.80);
	obj.gradian_to_degree(1000.50);
	return 0;
}

Output

Gradian : 1 Degree : 0.9
Gradian : 70.8 Degree : 63.72
Gradian : 1000.5 Degree : 900.45
/*
  C# program
  Convert gradian to degree 
*/
using System;
class MyMath
{
	//Find the degree of given gradian
	public void gradian_to_degree(double gradian)
	{
		// Formula : (gradian*180)/200
		// Calculate given gradian to degree 
		double degree = (gradian * 180) / 200;
		Console.Write("Gradian : " + gradian + " Degree : " + degree + "\n");
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		//Simple test
		obj.gradian_to_degree(1);
		obj.gradian_to_degree(70.80);
		obj.gradian_to_degree(1000.50);
	}
}

Output

Gradian : 1 Degree : 0.9
Gradian : 70.8 Degree : 63.72
Gradian : 1000.5 Degree : 900.45
<?php
/*
  Php program
  Convert gradian to degree 
*/
class MyMath
{
	//Find the degree of given gradian
	public	function gradian_to_degree($gradian)
	{
		// Formula : (gradian*180)/200
		// Calculate given gradian to degree 
		$degree = ($gradian * 180) / 200;
		echo "Gradian : ". $gradian ." Degree : ". $degree ."\n";
	}
}

function main()
{
	$obj = new MyMath();
	//Simple test
	$obj->gradian_to_degree(1);
	$obj->gradian_to_degree(70.80);
	$obj->gradian_to_degree(1000.50);
}
main();

Output

Gradian : 1 Degree : 0.9
Gradian : 70.8 Degree : 63.72
Gradian : 1000.5 Degree : 900.45
/*
  Node Js program
  Convert gradian to degree 
*/
class MyMath
{
	//Find the degree of given gradian
	gradian_to_degree(gradian)
	{
		// Formula : (gradian*180)/200
		// Calculate given gradian to degree 
		var degree = (gradian * 180) / 200;
		process.stdout.write("Gradian : " + gradian + " Degree : " + degree + "\n");
	}
}

function main()
{
	var obj = new MyMath();
	//Simple test
	obj.gradian_to_degree(1);
	obj.gradian_to_degree(70.80);
	obj.gradian_to_degree(1000.50);
}
main();

Output

Gradian : 1 Degree : 0.9
Gradian : 70.8 Degree : 63.72
Gradian : 1000.5 Degree : 900.45
#   Python 3 program
#   Convert gradian to degree 

class MyMath :
	# Find the degree of given gradian
	def gradian_to_degree(self, gradian) :
		#  Formula : (gradian*180)/200
		#  Calculate given gradian to degree 
		degree = (gradian * 180) / 200
		print("Gradian : ", gradian ," Degree : ", degree ,"\n", end = "")
	

def main() :
	obj = MyMath()
	# Simple test
	obj.gradian_to_degree(1)
	obj.gradian_to_degree(70.80)
	obj.gradian_to_degree(1000.50)

if __name__ == "__main__": main()

Output

Gradian :  1  Degree :  0.9
Gradian :  70.8  Degree :  63.72
Gradian :  1000.5  Degree :  900.45
#   Ruby program
#   Convert gradian to degree 

class MyMath

	# Find the degree of given gradian
	def gradian_to_degree(gradian)
	
		#  Formula : (gradian*180)/200
		#  Calculate given gradian to degree 
		degree = (gradian * 180) / 200
		print("Gradian : ", gradian ," Degree : ", degree ,"\n")
	end
end
def main()

	obj = MyMath.new()
	# Simple test
	obj.gradian_to_degree(1)
	obj.gradian_to_degree(70.80)
	obj.gradian_to_degree(1000.50)
end
main()

Output

Gradian : 1 Degree : 0
Gradian : 70.8 Degree : 63.72
Gradian : 1000.5 Degree : 900.45
/*
  Scala program
  Convert gradian to degree 
*/
class MyMath
{
	//Find the degree of given gradian
	def gradian_to_degree(gradian: Double): Unit = {
		// Formula : (gradian*180)/200
		// Calculate given gradian to degree 
		var degree: Double = (gradian * 180) / 200;
		print("Gradian : " + gradian + " Degree : " + degree + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		//Simple test
		obj.gradian_to_degree(1);
		obj.gradian_to_degree(70.80);
		obj.gradian_to_degree(1000.50);
	}
}

Output

Gradian : 1.0 Degree : 0.9
Gradian : 70.8 Degree : 63.72
Gradian : 1000.5 Degree : 900.45
/*
  Swift program
  Convert gradian to degree 
*/
class MyMath
{
	//Find the degree of given gradian
	func gradian_to_degree(_ gradian: Double)
	{
		// Formula : (gradian*180)/200
		// Calculate given gradian to degree 
		let degree: Double = (gradian * 180) / 200;
		print("Gradian : ", gradian ," Degree : ", degree ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	//Simple test
	obj.gradian_to_degree(1);
	obj.gradian_to_degree(70.80);
	obj.gradian_to_degree(1000.50);
}
main();

Output

Gradian :  1.0  Degree :  0.9
Gradian :  70.8  Degree :  63.72
Gradian :  1000.5  Degree :  900.45

Time Complexity

The time complexity of this program is constant for each conversion, as it involves only basic arithmetic operations like multiplication and division. Regardless of the value of the input gradians, the number of operations remains the same. Therefore, the time complexity can be considered O(1).

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