简体   繁体   中英

factorial in java using for loop

 /* this program 
  * finds the factorial for any number
  */

public class forLoop1{

    public static void main(int x){    
        int init; 
        for( init = x; init < 2; init--){
            int finalint = init * --init; 
            System.out.println(finalint); 
        }
    }
}

^^ This program doesn't have an output, can anyone think of what's going wrong here? Any help would be appreciated, thank you!

If you want to use command line argument then parse String to int .

public static void main(String args[]) {
    int n=Integer.parseInt(args[0]);
    int fac = 1;
    for(int i = n; i >= 2; i--) {
        fac = fac * i;
    }
    System.out.println(fac);
}

You should run this program as java forLoop1 5 for an input 5 for example.

You have problems at -- ummmm -- almost each line. Have a look at the code below and trace each step with n = 4 (or any other number).

public class forLoop1{
  public static void main(String[] args){    
    // n is the number whose factorial is to be calculated
    int n = 10; 
    int factorial = 1;
    for(int i = n; i >= 2; i--){
      factorial = factorial * i;
    }
    System.out.println(factorial);
  }
}
import java.util.Scanner;

public class forLoop1{

 public static void main(String args[])
  { 
  System.out.println("Enter a number greater than zero.");
  Scanner in = new Scanner(System.in);

  int n = in.nextInt();
  in.close();

  int fact = 1;
  for (n = n; n>=2; n--)
    fact *= n;

  System.out.println("Factorial of "+n+" is = "+fact);
  }
}

This should work. will read user input.将读取用户输入。

In your code, the loop will only execute if init is 2 to start with.2 时执行。

Here is an explanation of the errors in your code:

public static void main(int x){ //Should be public static void main(String[] args)       
    int init; 
    for( init = x; init < 2; init--){ //Should be init >= 2
        int finalint = init * --init; //Possibly should be init * (init - 1)
                                      //In each iteration of the loop,      
                                      //finalint will be overwritten
    System.out.println(finalint); //This line is fine.
}

Thanks for the answers! Arjun, great your logic works perfectly A few things though: I do not need the (String[] args) part; I mentioned before too, as my IDE takes input for the x value by itself. So this is the code I used in the end:

  /* Factorial 
   * program
   */


public class forLoop2 {

    public void main(long x) { // long x is correct, as my IDE takes input through this 
        long fact = 1; 

        for( x = x; x >= 2; x--) 
            fact = fact * x; 

        System.out.println(fact); 
    }
}
import java.util.*;

public class forLoop1{
    public static void main(String args[]){ 

        Scanner input=new Scanner(System.in);
        System.out.println("Enter the number:");
        int x=input.nextInt();

        int finalint=1;
        for( int init=1;init<=x;init++){
            finalint=finalint*init;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM