D – Compute a power b

Given two integers a and b, compute a power b.
Note: Do not use any inbuilt functions/libraries for your main logic.

Input Format

First and only line of input contains two positive integers a and b.

Constraints

1 <= a <= 100
0 <= b <= 9

Output Format

Print a power b.

InputcopyOutputcopy
2 38

Explanation :

Self Explanatory.

Solutions:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String[] values = input.split(" ");
        long a = Integer.parseInt(values[0]);
        long b = Integer.parseInt(values[1]);
        long result = 1;
        for (int i = 0; i < b; i++) {
            result *= a;
        }
        System.out.println(result);
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top