Java程序计算数字的幂
从用户读取基值和指数值。将基数本身乘以基数,再将结果乘以基数(再次)重复n次,其中n是指数值。
2 ^ 5 = 2 X 2 X 2 X 2 X 2 (5 times)
示例
import java.util.Scanner;
public class PowerOfNumber {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base number ::");
int base = sc.nextInt();
int temp = base;
System.out.println("Enter the exponent number ::");
int exp = sc.nextInt();
for (int i=1; i<exp; i++){
temp = temp*temp;
}
System.out.println("Result of "+base+" power "+exp+" is "+temp);
}
}输出结果
Enter the base number :: 12 Enter the exponent number :: 2 Result of 12 power 2 is 144
热门推荐
10 入伙英文祝福语大全简短
11 逗比婚礼祝福语简短
12 50多岁生日简短祝福语
13 老板端午简短祝福语大全
14 嫁女儿祝福语女生简短
15 老板茶楼开业祝福语简短
16 思念丈夫祝福语简短的话
17 除夕的拜年祝福语简短
18 老师及家人祝福语简短