forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial using class.java
41 lines (31 loc) · 1.12 KB
/
factorial using class.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
// Class to calculate factorial
class FactorialCalculator {
// Method to calculate factorial
public int calculateFactorial(int number) {
int factorial = 1;
// Loop to calculate factorial
for (int i = 1; i <= number; i++) {
factorial *= i;
}
return factorial;
}
}
// Main class
public class FactorialMain {
public static void main(String[] args) {
// Create scanner object to take input
Scanner scanner = new Scanner(System.in);
// Create object of FactorialCalculator class
FactorialCalculator calculator = new FactorialCalculator();
// Take user input
System.out.print("Enter a number to calculate its factorial: ");
int number = scanner.nextInt();
// Calculate factorial using the class method
int result = calculator.calculateFactorial(number);
// Print the result
System.out.println("The factorial of " + number + " is: " + result);
// Close scanner
scanner.close();
}
}