Create a static method
public static int pow(int base, int exp)
that returns the power value of base raised to its exponent. Use this method in a program that first asks for a base value and an exponent value, calls pow() and then prints the result.
Sample output:
Enter the base: 2
Enter the exponent: 3
The power value is 8
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static int removeSpace(String str){
str = str.replaceAll(" ", "");
System.out.println(str);
return str.length();
}
public static int[] Killed(int[] arrayDragon, Scanner in){
int ch = in.nextInt();
for (int i = 0; i < arrayDragon .length; i++) {
if(ch == arrayDragon [i]){
arrayDragon [i] = 0;
}
}
return arrayDragon;
}
public static int pow(int base, int exp){
int rez = 1;
for(int i = 0; i < exp; i++)
rez *= base;
return rez;
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.print("Enter the base: ");
int base = in.nextInt();
System.out.print("Enter the exponent: ");
int exp = in.nextInt();
System.out.println("The power value is " + pow(base, exp));
}
}
Comments
Leave a comment