Java Program To Find Factorial Of a Given Number


package com.java2study;

import java.util.Scanner;

public class FactorialDemo {
int factorial = 1;
public void calculateFact(int number){
for(int i = 1;i<=number;i++){
factorial = factorial*i;
}
System.out.println(factorial);

}
public static void main(String[] args) {
// accept input from user we need to call Scanner classes
Scanner scanner = new Scanner(System.in);
System.out.println("Enter A Number To Find Factorial:");
//Store the numeric value to number
int number = scanner.nextInt();

FactorialDemo factDemo = new FactorialDemo();
factDemo.calculateFact(number);

}

}

output


Enter A Number To Find Factorial:
0
1
--------------------
Enter A Number To Find Factorial:
1
1
---------------
Enter A Number To Find Factorial:
2
2
----------------
Enter A Number To Find Factorial:
3
6
---------
Enter A Number To Find Factorial:
4
24
-----------
Enter A Number To Find Factorial:
5
120

No comments:

Post a Comment