java - How can I add exponent in my code -
import java.util.scanner; public class calculator { public static void main(string[] args ) { scanner userinput = new scanner(system.in); string operator; double num1,num2,answer = 0; system.out.println("enter first number: "); num1 = userinput.nextdouble(); system.out.println("enter operator: "); operator = userinput.next(); system.out.println("enter second number: "); num2 = userinput.nextdouble(); if (operator.equals ("+")){ answer = num1 + num2; } else if (operator.equals ("-")){ answer = num1 - num2; } else if (operator.equals ("*")){ answer = num1 * num2; } else if (operator.equals ("/")){ answer = num1 / num2; } system.out.println("first number:" + num1); system.out.println("operator:" + operator); system.out.println("second number:" + num2); system.out.println("answer: " + answer); } }
use function:
math.pow(x, y)
in way: place code
else if (operator.equals ("^")){ answer = math.pow(num1, num2); }
just after present code
else if (operator.equals ("/")){ answer = num1 / num2; }
so part of code be
else if (operator.equals ("/")){ answer = num1 / num2; } else if (operator.equals ("^")){ answer = math.pow(num1, num2); }
Comments
Post a Comment