What is wrong sin cos not wark | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

What is wrong sin cos not wark

import java.lang.Math; import java.util.Scanner; public class calculater { public static void main(String[ ] args) { Scanner input=new Scanner (System .in); System .out.println ("Enter 1st num"); int a=input.nextInt(); System .out.println ("Enter 2st num"); int b=input.nextInt(); System .out.println ("Enter operation"); String op=input.next(); //int z; if(op.equals("+")){ System.out.println(a+b); } else if(op.equals("-")){ System.out.println(a-b); } else if(op.equals("*")){ System.out.println(a*b); } else if (op.equals("/")){ System.out.println(a/b); } else if (op.equals("sin")){ System.out.println("sin =" +Math.sin(num)); } else if (op.equals("cos")){ System.out.println("cos =" +Math.cos(num)); } else if (op.e

6th Feb 2021, 5:18 PM
Dark Light
Dark Light - avatar
4 Answers
+ 1
( 1 ) Seems like your 'num' variable is missing and not defined (In this fix, I just added a temporary 'num' value because I dont exactly know what 'num' would you want) ( 2 ) Scanner input=new Scanner (System. in); // should be Scanner input=new Scanner (System.in); // to read input properly. If this is still unclear, please feel free to ask. Thanks. https://code.sololearn.com/ca25a8a4A81A
6th Feb 2021, 5:28 PM
noteve
noteve - avatar
+ 1
You're likely expecting it to interpret your angle using degree units. It actually interprets the angle in radians. You can convert like this: Math.sin(numDegrees * Math.PI / 180); You could do something like this: import java.util.Scanner; public class calculater { private static boolean isOneOperandOperation(String op) { return op.equals("sin") || op.equals("cos") || op.equals("tan"); } public static void main(String[ ] args) { Scanner input=new Scanner (System .in); System .out.println ("Enter 1st num"); int a=input.nextInt(); System .out.println ("Enter operation"); String op=input.next(); if (isOneOperandOperation(op)) { // convert a from degrees to radians. double angleInRadians = a * Math.PI / 180; if (op.equals("sin")) System.out.println(Math.sin(angleInRadians)); else if (op.equals("cos")) System.out.println(Math.cos(angleInRadians)); else System.out.println(Math.tan(angleInRadians)); } else { System.out.println ("Enter 2stnum"); int b=input.nextInt(); if(op.equals("+")){ System.out.println(a+b); } else if(op.equals("-")){ System.out.println(a-b); } else if(op.equals("*")){ System.out.println(a*b); } else if (op.equals("/")){ System.out.println(a/b); } } } }
6th Feb 2021, 9:52 PM
Josh Greig
Josh Greig - avatar
0
Can you please tell me how you are gonna give input for sin and cos?
6th Feb 2021, 5:25 PM
Atul [Inactive]
0
I remember that we should convert num to radians a=60; num= Math.toRadians(a); System.out.println(Math.sin(num));
6th Feb 2021, 5:34 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar