Power of Number

Compute a^b using a loop (similar to earlier power program).

JavaBeginner
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter base: ");
        int a = sc.nextInt();
        System.out.print("Enter exponent: ");
        int b = sc.nextInt();

        long result = 1;
        for (int i = 1; i <= b; i++) {
            result *= a;
        }

        System.out.println(a + " ^ " + b + " = " + result);
        sc.close();
    }
}

Output

Enter base: 3
Enter exponent: 4
3 ^ 4 = 81

We multiply the base by itself b times; demonstrates loop-based exponentiation.