How do I make my calculator this to calculate in degrees rather than radians

95 Views Asked by At

I have been stuck on this for a very long bit of time. How do I make my calculator calculate in degrees rather than radians. I tried Math.toDegrees but it did not work. Thank you if you decide to help.

import java.util.Scanner;

public class Main {

 public static void main(String[] args) {
     
  Scanner scanner = new Scanner(System.in);
  

  // END USER INPUT VALUE IN DEGREES
  System.out.println("Please input a value to recieve the Trigonemetric value in degrees ");

  double degrees = scanner.nextDouble();
  
  double sineOfAngle = Math.sin(degrees); 
  double cosOfAngle = Math.cos(degrees); 
  double tanOfAngle = Math.tan(degrees);

  System.out.println();
  System.out.println("The Sine of " + degrees + " degrees is : "
    + sineOfAngle);
  System.out.println("The Cosine of " + degrees + " degrees is : "
    + cosOfAngle);
  System.out.println("The Tangent of " + degrees + " degrees is : "
    + tanOfAngle);
    
 }
}
2

There are 2 best solutions below

4
ATP On BEST ANSWER

use Math.toRadians(), You want to convert the degrees to radians because the Math trigonometric functions parameters should be in radians:

public class Main {

 public static void main(String[] args) {
     
  Scanner scanner = new Scanner(System.in);
  

  // END USER INPUT VALUE IN DEGREES
  System.out.println("Please input a value to recieve the Trigonemetric value in degrees ");

  double degrees = scanner.nextDouble();
  
  double sineOfAngle = Math.sin(Math.toRadians(degrees)); 
  double cosOfAngle = Math.cos(Math.toRadians(degrees)); 
  double tanOfAngle = Math.tan(Math.toRadians(degrees));

  System.out.println();
  System.out.println("The Sine of " + degrees + " degrees is : "
    + sineOfAngle);
  System.out.println("The Cosine of " + degrees + " degrees is : "
    + cosOfAngle);
  System.out.println("The Tangent of " + degrees + " degrees is : "
    + tanOfAngle);
    
 }
}
0
Aleksandar On

Why you need that? Programming language will always use radians for calculation. You can accept input in degrees, then convert it to radians, make calculation and convert it back to degrees on output.