0 votes
1 view
in Java by (9.2k points)

Below is the code which prompts the user to enter two numbers as ‘3’ and ‘2’ for example, it will give 9. 

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);

    }

}

Can anyone tell me if Java has an exponential operator to do the same? 

1 Answer

0 votes
by (19k points)

Check the below code implementation which uses math.pow: 

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 " + 

        (int) Math.pow(first, second));    // outputs 9

Interested in Java? Check out this Java tutorial by Intellipaat. 

Related questions

0 votes
1 answer
asked Feb 12 in Java by Jake (6.8k points)
0 votes
1 answer
+1 vote
2 answers
asked Jul 4, 2019 in Java by prachi95 (1.1k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...