user3362992 user3362992 - 4 months ago 22
Java Question

Exponential operator in java?

is there any operator which works as a exponential operator??
prompt users to put two numbers..
let's say someone typed 3 2 then the correct answer is 9..how can i do this?

import java.util.Scanner;
public class Exponentiation {

public static double powerOf (double p){
double pCubed;

pCubed = p*p;
return (pCubed);
}

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

double num = 2.0;
double cube;

System.out.print ("Please put two numbers: ");
num = in.nextInt();

cube = powerOf(num);

System.out.println (cube);
}
}

Answer

To do this with user input:

public static void getPow(){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter first integer: ");    // 3
    int first = sc.nextInt();
    System.out.println("Enter second integer: ");    // 2
    int second = sc.nextInt();
    System.out.println(first + " to the power of " + second + " is " + 
        Math.pow((double)first, (double)second));    // outputs 9