81

Is there an exponential operator in Java?

For example, if a user is prompted to enter two numbers and they enter 3 and 2, the correct answer would be 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);
    }
}
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
user3362992
  • 843
  • 1
  • 6
  • 4

6 Answers6

135

There is no operator, but there is a method.

Math.pow(2, 3) // 8.0

Math.pow(3, 2) // 9.0

FYI, a common mistake is to assume 2 ^ 3 is 2 to the 3rd power. It is not. The caret is a valid operator in Java (and similar languages), but it is binary xor.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 4
    I wonder why `^` is the de factor standard for the bitwise XOR operator (e.g. Java & Python) - must have confused many a beginners or MATLAB users... – flow2k Aug 09 '18 at 02:34
  • 4
    Why single out java and python? `^` as bitwise xor in a general purpose procedural programming languages was introduced by C, and virtually any language based on C (or that took inspiration from C or C++) has followed that convention. The fact that it means something else in a special-purpose maths programming language shouldn't really confuse anyone? – Mike 'Pomax' Kamermans Nov 19 '19 at 17:46
44

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 " + 
        (int) Math.pow(first, second));    // outputs 9
didxga
  • 5,935
  • 4
  • 43
  • 58
Jason Pather
  • 1,127
  • 2
  • 12
  • 18
7

The easiest way is to use Math library.

Use Math.pow(a, b) and the result will be a^b

If you want to do it yourself, you have to use for-loop

// Works only for b >= 1
public static double myPow(double a, int b){
    double res =1;
    for (int i = 0; i < b; i++) {
        res *= a;
    }
    return res;
}

Using:

double base = 2;
int exp = 3;
double whatIWantToKnow = myPow(2, 3);
libik
  • 22,239
  • 9
  • 44
  • 87
  • how do i prompt users to put two numbers? i.e 2 3 = 8 – user3362992 Feb 28 '14 at 01:57
  • in your example base will be 3 and exponent 2. I guess you should be multiplying by a inside loop and iterate b times instead. – Saad Aug 01 '16 at 19:55
  • 1
    @libik What if b is negative ? – shanwu Feb 10 '17 at 05:10
  • 1
    The myPow(double, double) method signature implies it calculates with floating point powers while in fact it doesn't. The method signature should read myPow(double, int). Math.pow(double, double) does take floating point powers into account, e.g., Math.pow(4, 0.5) returns the square of 4, so 2, whereas myPow(4, 0.5) would return 4. – Hummeling Engineering BV Sep 13 '17 at 13:19
  • @HummelingEngineeringBV - ok, lets fix it after more than 4 years :) – libik Dec 15 '17 at 14:36
6

There is the Math.pow(double a, double b) method. Note that it returns a double, you will have to cast it to an int like (int)Math.pow(double a, double b).

PlasmaPower
  • 1,864
  • 15
  • 18
4

you can use the pow method from the Math class. The following code will output 2 raised to 3 (8)

System.out.println(Math.pow(2, 3));
3

In case if anyone wants to create there own exponential function using recursion, below is for your reference.

public static double power(double value, double p) {
        if (p <= 0)
            return 1;

        return value * power(value, p - 1);
    }
Rajesh
  • 4,273
  • 1
  • 32
  • 33
  • 4
    please don't suggest such algorithms, it will only work for a negligible subset of p's domain – Markus Sep 09 '21 at 06:30