-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormal.java
29 lines (24 loc) · 859 Bytes
/
normal.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
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Prompt the user for a number
System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();
// Check if the number is non-negative
if (number < 0) {
System.out.println("Factorial is not defined for negative numbers.");
} else {
// Calculate the factorial
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
// Display the result
System.out.println("Factorial of " + number + " is " + factorial);
}
// Close the scanner
scanner.close();
}
}