Java 程序:计算一个数的阶乘

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

在本教程中,我们将学习如何在 java 中找到一个数的阶乘。一个数的阶乘是从 1 到该数的所有整数的乘积。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。

输入:输入数字:5

输出:输入数字的阶乘为:120

程序 1:求一个数的阶乘

在这个程序中,我们将学习如何使用 while 循环找到一个数的阶乘。

算法

  1. 开始

  2. 创建扫描仪类的实例。

  3. 声明一个变量。

  4. 要求用户初始化变量。

  5. 声明一个循环变量和另一个变量来存储数字的阶乘。

  6. 将两个变量都初始化为 1。

  7. 使用 while 循环计算阶乘。

  8. 运行循环,直到循环变量小于或等于该数字。

  9. 在每次迭代中更新阶乘。

  10. 在每次迭代中增加循环变量。

  11. 打印数字的阶乘。

  12. 停下来。

下面是用 Java 打印一个数字的阶乘的代码示例。

//Java Program to find the Factorial of a Number
import java.util.*;
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);
        //Declare and Initialize the variable
        System.out.println("Enter the number: ");
        int num=sc.nextInt();
        int i=1,fact=1;
        while(i<=num)
        {
            fact=fact*i;
            i++;
        }
        System.out.println("Factorial of the number: "+fact);  
     }   
}

输入数字:5 数字的阶乘:120

Java 程序:程序 2:寻找一个数的阶乘

在这个程序中,我们将学习如何使用 for 循环来求一个数的阶乘。

算法

  1. 开始

  2. 创建扫描仪类的实例。

  3. 声明一个变量。

  4. 要求用户初始化变量。

  5. 声明一个变量来存储数字的阶乘。

  6. 将变量初始化为 1。

  7. 使用 for 循环计算阶乘。

  8. 通过在每次迭代中将其与循环变量相乘来更新阶乘变量。

  9. 打印数字的阶乘。

  10. 停下来。

下面是用 Java 打印一个数字的阶乘的代码示例。

//Java Program to find the Factorial of a Number
import java.util.*;
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);
        //Declare and Initialize the variable
        System.out.println("Enter the number: ");
        int num=sc.nextInt();
        int fact=1;
        for(int i=1;i<=num;i++)
        {
            fact=fact*i;
        }
        System.out.println("Factorial of the number: "+fact); 
     }  
}

输入数字:4 数字的阶乘:24

Java 程序:程序 3:寻找一个数的阶乘

在这个程序中,我们将使用带有用户定义值的递归找到一个数的阶乘。这里,我们将要求用户输入一个值,然后通过递归调用函数来计算阶乘。

算法

  1. 开始

  2. 声明一个变量来存储一个数字。

  3. 要求用户初始化数字。

  4. 检查是否可以计算阶乘。

  5. 如果数字大于等于 0,则调用递归函数来计算输入数字的阶乘。

  6. 如果数字小于 0,则打印无法计算阶乘的消息。

  7. 如果输入的数字是 0 或 1,则返回 1。

  8. 如果输入的数字不是 0 或 1,则通过递归调用相同的方法计算阶乘。

  9. 返回结果。

  10. 打印输入数字的阶乘。

  11. 停止

下面是用 Java 打印一个数字的阶乘的代码示例。

/*Java Program to find factorial of a number using Recursive Function*/
import java.util.Scanner;
public class Main
{
    //Driver Code
    public static void main(String[] args) 
    {
        //Take input from the user
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number :");
        int num = sc.nextInt();   //Input the number
        if(num>=0) 
        {
           //Call a recursive function to find the factorial
           int factorial=findFactorial(num);
           System.out.println("The factorial of the entered number is :"+factorial);
        }        
        else
        {
            System.out.println("Factorial not possible.");
            System.out.println("Please enter valid input.");
        } 
    }
    //Recursive Function to Find the Factorial of a Number
    public static int findFactorial(int num)
    {
        if(num==0)
        return 1;
        else if(num==1)
        return 1;
        else
        return num*findFactorial(num-1);        
    }
}

输入数字:8 输入数字的阶乘为:40320