阶乘 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21482785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:56:59  来源:igfitidea点击:

Factorial Java Program

javafor-loopfactorial

提问by Eqorip

I want to do a factorial program in java using a for loop. For example I want to take the user input, lets say 10, and then multiply 10*9*8*7*6*5*4*3*2*1. I need help constructing the forloop. The code below is all I have so far as I'm not sure where to go after.

我想使用for循环在java中做一个阶乘程序。例如,我想接受用户输入,比如说10,然后乘以10*9*8*7*6*5*4*3*2*1。我需要帮助构建for循环。下面的代码是我目前所拥有的所有代码,因为我不知道该去哪里。

import java.util.Scanner;
import java.lang.Math;
public class factorial {

    public static void main(String[] args) {
        int num;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        num = input.nextInt();
    }
}

采纳答案by laaposto

Try

尝试

public static void main(String[] args) {
    int num;
    int fact=1;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    num = input.nextInt();
    for (int i=2;i<=num; i++){
        fact=fact*i;
    }

    System.out.println("Factorial: "+fact);
}

As @Marko Topolnik mentioned in comments this code will work for inputs up to 12. For larger inputs will output infinity due to overflow.

正如@Marko Topolnik 在评论中提到的,此代码适用于最多 12 的输入。对于更大的输入,由于溢出将输出无穷大。

For numbers larger than 12 you should use higher data type like BigInteger

对于大于 12 的数字,您应该使用更高的数据类型,例如 BigInteger

You can try:

你可以试试:

public static void main(String[] args) {
    BigInteger num;
    BigInteger fact = BigInteger.valueOf(1);
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    num = input.nextBigInteger();
    for (int i = 2; i <= num; i++){
        fact = fact.multiply(BigInteger.valueOf(i));
    }
    System.out.println(fact);
}

回答by Craig Manson

int sum = 1;

for (int i = 2;i <= num;i++) {
    sum = sum * i;
}

回答by Fabinout

Why bother computing it?

为什么要费心计算呢?

public int factorial ( int n ) {
switch(n){
case 0: return 1;
case 1: return 1;
case 2: return 2;
case 3: return 6;
case 4: return 24;
case 5: return 120;
case 6: return 720;
case 7: return 5040;
case 8: return 40320;
case 9: return 362880;
case 10: return 3628800;
case 11: return 39916800;
case 12: return 479001600;
default : throw new IllegalArgumentException();
}

Source : @Emory

资料来源:@埃默里

回答by Siddhesh Urkude

Try this program:=

试试这个程序:=

import java.util.*;
public class Factorials {
 public static void main(String[] args){
  Scanner sc=new Scanner(System.in);
  int i,num,a,b;
  int c=1;
  System.out.println("Enter the no.= ");
  num=sc.nextInt();
  for(i=1;i<=num;i++){
   c=i*c;
  }
  System.out.println("Factorial is "+c);
 }
}

回答by Merit Campus

This is My Program

这是我的程序

class FactorialUsingFor
{
    public static void main(String arg[])
    {
        int factorial = 1;
        int number = 6;

        for(int i = 1; i <= number; i++)
        {
            factorial *= i;
        }

        System.out.println("Factorial of number " + number + " is " + factorial);

    }
}

回答by gadolf

Coding by Recursive function:

递归函数编码:

import java.util.Scanner;

public class Main {

     public static void main(String[] args)
     {
         System.out.print("Enter a number: ");
         Scanner input = new Scanner(System.in);
         int num = input.nextInt();
         long factorialResult = factorialRecursive(num);
         System.out.println(factorialResult);
     }


     public static long factorialRecursive(int n) {
         if (n == 0 || n == 1 )
             return 1;

         return n * factorialRecursive(n - 1);
     }
}

source: learn.uncox.com

来源:learn.uncox.com

If the number is large, "stackoverflow" happens.

如果数量很大,就会发生“stackoverflow”。

回答by David William

import java.util.Scanner;
public class Factorial {
    public static void main(String[] args){
        Scanner scn = new Scanner(System.in);
        System.out.println("Enter the number to find factorial");
        int o=scn.nextInt();
        int count=0,b=o-1,res=(o*(o-1)),r=0;
        while(count!=1)
        {
         count=b-1;
         r=count*res;
         res=r;
         r=1;
         b=b-1;
        }
        System.out.println(res);
    }
}