簡體   English   中英

階乘 Java 程序

[英]Factorial Java Program

我想使用 for 循環在 java 中執行階乘程序。 例如,我想接受用戶輸入,比方說10 ,然后乘以10*9*8*7*6*5*4*3*2*1 我需要幫助構建for循環。 下面的代碼是我目前所擁有的全部代碼,因為我不確定 go 之后的位置。

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();
    }
}

遞歸函數編碼:

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);
     }
}

來源: learn.uncox.com

如果數量很大,就會發生“stackoverflow”。

嘗試

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);
}

正如@Marko Topolnik 在評論中提到的,此代碼適用於最多 12 的輸入。對於更大的輸入,由於溢出將輸出無窮大。

對於大於 12 的數字,您應該使用更高的數據類型,例如BigInteger

你可以試試:

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);
}

為什么要費心計算呢?

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();
}

資料來源: @埃默里

這是我的程序

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);

    }
}
int sum = 1;

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

試試這個程序:=

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);
 }
}
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);
    }
}
int factorialLoop(int val) {
     
     int result = 1 ;
    
    for (int i = val ; i>= 1 ; i--){
        
        result =  result * i ;
        
    }
    
    return result ;
    
}

int factorielRecursive(int val){ 
    
   if(1 <= val){
    
     return val*factorialRecursive(val-1);
     
   }else {
    
    return 1;
    
   }
    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM