-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQ_15.java
More file actions
47 lines (36 loc) · 1.26 KB
/
Q_15.java
File metadata and controls
47 lines (36 loc) · 1.26 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
package src;
// Muhammad Naveed
// (Factorial) The src.factorial of a non-negative integer n is written as
// n! (pronounced “n src.factorial”) and is defined as follows: n! = n · (n – 1) · (n – 2) · … · 1
// (for values of n greater than or equal to 1) and n! = 1 (for n = 0)
// For src.example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.
// Write an application that reads a non-negative integer and computes and prints its src.factorial.
import java.util.Scanner;
public class Q_15 {
private final Scanner sc = new Scanner(System.in);
private int number = 0;
public void getNumber() {
System.out.print("Enter the number : ");
number = sc.nextInt();
isValid();
}
private void isValid() {
if (number >= 0) {
factorial();
} else {
System.out.print("Invalid number! \nPlease try with some non-negative numbers.\n");
getNumber();
}
}
private void factorial() {
long facto = 1;
for (int i = number; i > 0; i--) {
facto *= i;
}
System.out.println("The src.factorial of " + number + " is " + facto);
}
public static void main(String[] args) {
Q_15 Obj = new Q_15();
Obj.getNumber();
}
}