In this example, I’ll show you How to Calculate Factorial in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter your number:"); int n=sc.nextInt(); int fact=1; for(int i=n;i>1;i--) { fact=fact*i; } System.out.println("factorial is:"+fact); } |
Output: