Best algorithm for calculating factorial

So, I'm learning to develop in Java. The professor asked me to develop a method for the factorial. And also show on the screen what is happening, for example:

3! = 3 X 2 X 1 = 6

In fact, I made it happen, but is there any way to make the code better?:

    int n = Integer.parseInt(txtN.getValue().toString());        
    int f = 1; 
    int c = n; 
    String s = ""; 

    while(c>=1){ 
        f *= c;
        if (c == 1) {
            s += c; 
        } else if (c>1) {
            s += c + " x " ;
        }
        c--;
    }

    lblFat.setText(s + " = " + Integer.toString(f));

The code generates the factorial and should also show the numbers on the screen.

Example

3! = 3 X 2 X 1 = 6
3! -> txtN (Local para o usuário inserir o número)
6 -> lblFat (local que gera o resultado)

Factorial in Java, do you have a better way to write code?

Author: Maniero, 2016-09-01

5 answers

The most common is to separate the calculation from the presentation, but in something simple it gives to understand. According to what was put in the question I would do so (in fact I would probably use a for):

int f = 1; 
int c = n; 
String s = ""; 
while (c > 1) { 
    f *= c;
    s += c + " x " ;
    c--;
}
s += c;

See working on ideone. E no repl.it. also I put on GitHub for future reference .

To do recursively would be much more complicated, even if it is not required .

Obviously using a simple integer there is a strict limit of the maximum that can calculate the factorial. I believe that for this exercise this is not a problem, the intention is not something of production but to understand the functioning of the algorithm, but it is worth knowing that in certain cases a BigInteger would fall better, as was commented by Bruno Bermann.

 10
Author: Maniero, 2020-11-06 19:24:08
public class calculeFatorial{
  public static void main(String[] args) {
    int fatorial = 1;

    for (int i = 1;i < 11 ; i ++ ) {
      fatorial *= i;

      System.out.println("Fatorial de " +i+"=" +fatorial);
    }
  }
}
 2
Author: Marcelo Batista, 2017-08-11 03:12:39

Guy you can do recursive.

Int FatorialRecursivo(int num){

   If(num==1) || (num==o)
      return 1;
   else
      return FatorialRecursivo(n-1)*n;
 }

Sorry the kind of messy code I'm responding to by the APP, anything asks if you didn't understand the recursion.

 1
Author: Nathan Martins, 2016-09-02 10:16:50

Presenting factorial with for:

long fat=1;
String mult="";
for (int i=5; i>1; i--){
    fat *=i;
    mult +=i+" x ";
}
System.out.print(mult+"1 = "+fat);

See running on Ideone

It is more recommended that the variable that accumulates the values be of type long, considering that the int has greater spectrum limitation. To change the value, just change the 5 by a variable and choose the way to feed it.

At the end, 1 was added outside the replay structure. Because it is factorial, multiplication is not affected by it, being able to withdraw it, saving a back in the calculations, only being aggregated in the string that had already received the " X " after the 2.

 1
Author: Jaider Xavier Jr., 2017-08-11 01:26:52
public int metodo(int valor){

        if(valor==1)
           return 1;

        else

         return (valor * metodo(valor -1)); //desmontando o numero 
               
    }
// UTLIZANDO RECURSIVIDADE
 -3
Author: WESLEY Mendes, 2020-07-04 13:53:04