Welcome to ICSE Java Answers. Ask any questions you have related to Java and we will answer them.
Java is a registered trademark of Oracle. This site is in no way related to or endorsed by Oracle.

Calculate a power b

0 votes
asked by anonymous
edited by Ranjith

1 Answer

0 votes

Program

import java.util.Scanner;

public class Power {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a: ");
        int a = scanner.nextInt();
        System.out.print("Enter b: ");
        int b = scanner.nextInt();
        int result = 1;
        for (int i = 1; i <= b; i++) {
            result = result * a;
        }
        System.out.println("a power b = " + result);
    }
}

Sample output

Enter a: 3
Enter b: 4
a power b = 81

 

answered by Ranjith (2,683 points)
Cant we use Math.pow(a,b);
@dipesh Yes, that will also work.
...