Java 程序:计算数的幂

原文:https://www.studytonight.com/java-programs/java-program-to-calculate-the-power-of-a-number

在本教程中,我们将学习如何在 java 中找到一个数的幂。一个数的幂被定义为通过将基值乘以 n 次而得到的值,其中 n 是指数值。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。

输入:输入基值:2

输入指数值:4

输出: 2 升至 4 次方为 16.0

上述问题可以通过以下方式解决:

方法 1:使用 While 循环

方法 2:使用 For 循环

方法 3:使用电源()

让我们分别看看这些方法

Java 程序:程序 1:计算一个数的幂

在这个程序中,我们将看到如何使用 while 循环计算一个数的幂。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 为基数和指数声明两个变量。
  4. 要求用户初始化这两个变量。
  5. 使用 while 循环计算一个数的幂。
  6. 打印计算值。
  7. 停止

下面是相同的代码。

//Java Program to Calculate the Power of a number
import java.util.Scanner;  
public class Main   
{  
    public static void main(String[] args)    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the base value: ");  
        int base = sc.nextInt();  
        System.out.println("Enter the exponent value: ");  
        int exp = sc.nextInt();  
        long result = 1;
        System.out.print(base+ " raised to the power "+ exp+" is: ");
        while (exp != 0)
        {
            result *= base;
            --exp;
        }
        System.out.println(result);
   }  
}

输入基值:2 输入指数值:3 2 的 3 次方是:8

Java 程序:程序 2:计算一个数的幂

在这个程序中,我们将看到如何使用 for 循环计算一个数的幂。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 为基数和指数声明两个变量。
  4. 要求用户初始化这两个变量。
  5. 使用 for 循环计算一个数的幂。
  6. 打印计算值。
  7. 停止

下面是相同的代码。

//Java Program to Calculate the Power of a number
import java.util.Scanner;  
public class Main   
{  
    public static void main(String[] args)    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the base value: ");  
        int base = sc.nextInt();  
        System.out.println("Enter the exponent value: ");  
        int exp = sc.nextInt();  
        long result = 1;
        System.out.print(base+ " raised to the power "+ exp+" is: ");
        for (;exp != 0; --exp)
        {
            result *= base;
        }
        System.out.println(result);
   }  
}

输入基值:3 输入指数值:3 3 的 3 次方是:27

Java 程序:程序 3:计算一个数的幂

在这个程序中,我们将看到如何使用幂()计算一个数的幂。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明两个变量。
  4. 要求用户初始化变量。
  5. 使用 Math.pow()计算数字的幂。
  6. 打印数字的幂的值。
  7. 停止

下面是相同的代码。

//Java Program to Calculate the Power of a number
import java.util.Scanner;  
public class Main   
{  
    public static void main(String[] args)    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the base value: ");  
        int base = sc.nextInt();  
        System.out.println("Enter the exponent value: ");  
        int exp = sc.nextInt();  
        System.out.print(base+ " raised to the power "+ exp+" is: ");
        double result = Math.pow(base, exp);
        System.out.println(result);
   }  
}

输入基值:8 输入指数值:2 8 的 2 次方是:64.0