Here you will get program for factorial in java.
We can find factorial of any number by multiplying it with all the numbers below it.
For example, factorial of 4 is 4*3*2*1 = 24.
Program for Factorial in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com; import java.util.Scanner; class Factorial { public static void main(String...s) { int n,fac=1,i; Scanner sc = new Scanner(System.in); System.out.println("Enter a number: "); n = sc.nextInt(); for(i=1;i<=n;i++) fac*=i; System.out.println("\nFactorial = "+fac); sc.close(); } } |
Output
Enter a number:
6
Factorial = 720
without package aur util.out se program run ho gayega