-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfive.java
More file actions
18 lines (18 loc) · 708 Bytes
/
five.java
File metadata and controls
18 lines (18 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Write a program to compute & print factorial of any given number.
import java.util.Scanner;
public class fifth {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int num = scanner.nextInt();
if (num < 0) {
System.out.println("Factorial is not defined for negative numbers.");
} else {
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
}
}
}