import java.util.*;

public class factorial
{
	public static void main(String[]args)
	{
		int m,f,n;
		System.out.println("Factorial Solver");
		System.out.println("Enter a number");
		Scanner sc = new Scanner(System.in);
     	n = sc.nextInt();
     	m = 1;
     	f = 1;
     	f = f * m;
     	while (m != n)
     	{
     		m = m + 1;
     	}
     	f = f * m;
     	System.out.println(f);
 
	}
}

Whenever I run it, it does not come out with the factorial. It just prints out the inputted value most of the time. Why is that?

have you tried using a for loop to find the factorial of an input number

I used a while loop. Is that wrong?

technically, no

but if it doesn't work using a while loop, it doesn't hurt to try using a for loop

use for loop. It will be conceptually easier.

int F =5;
int R = 1;
for i = F to i >1 ; i--
{
    R equals R times F;
    F equals F minus 1;
}
public class FactorialFinder {

	public static void main(String[] args) {
		int counter = 5;
		int number = counter;
		long factorial = counter;
		while (counter > 1)
			factorial *= --counter;
		System.out.println("Factorial of " + number + " is " + factorial);
	}
}

SNIP

public class Factorial {
    public static int result=1;
    
    public static void getFactorial(int n){
	if(n>0){
	    result*=(n--);
	    getFactorial(n);
	}
    }
    
    public static void main(String args[]){
	getFactorial(4);
	System.out.println(result);
    }
}

You can also try this solution if your number is positive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.