-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-04-Java-Q24
More file actions
58 lines (41 loc) · 1.2 KB
/
Day-04-Java-Q24
File metadata and controls
58 lines (41 loc) · 1.2 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*A factorial is the product of all the natural numbers less than or equal to the given number n.Sheela wants to know how its working in programming.
For Example,
The factorial of 6 is 6 * 5 * 4 * 3 * 2 * 1 which is 720.
could you please help her to write the program to find the factorial of a number.
Input Format
Input consists of one integer
Constraints
Given N is non Negative values.
Output Format
print the factorial Values
Sample Input 0
5
Sample Output 0
The Factorial of 5 is 120
Sample Input 1
6
Sample Output 1
The Factorial of 6 is 720
Sample Input 2
-4
Sample Output 2
Invalid Input*/
#Answer
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
if (n < 0) {
System.out.println("Invalid Input");
} else {
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println("The Factorial of " + n + " is " + factorial);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
}