To find a number factorial in java , Follow the process.

Go the series : https://browncodeit.blogspot.com/p/java-programs.html

Source Code:

  1. public class FactorialDemo {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner(System.in)) {
  4. int n,i,fact = 1; //variable declaration.
  5. System.out.println("Enter the number: ");
  6. n = input.nextInt(); //take input in n variable.
  7. for(i=n;i>=1;i--) {
  8. fact = fact*i;
  9. }
  10. System.out.println("factorial is :"+fact);
  11. }
  12. }
  13. }

Output:

Enter the number: 5
factorial is :120

Enter the number: 8
factorial is :40320

0 comments:

Post a Comment