how to solve 10 factorial java code example

Example 1: Factorial from 1 to 10 in java

// Factorial from 1 to 10 in java
public class FactorialFrom1To10
{
   public static void main(String[] args)
   {
      int count;
      long factorial = 1;
      System.out.printf("%4s%30s\n", "Number", "Factorials");
      for(count = 1; count <= 10; count++)
      {
         factorial *= count;
         System.out.printf("%4d%,30d\n", count, factorial);
      }
   }
}

Example 2: how to make factorial in java

class fact{
	static int fac(int n){
		int ans =1;
		for(int i=1; i<=n; i++){
			ans = ans*i;
		}
		return ans;
	}

	public static void main (String[] args){
		int f = 5;
		System.out.println(fac(f));
	}
}

Tags:

Java Example