-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab4.java
42 lines (30 loc) · 938 Bytes
/
Lab4.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
42
import java.util.Scanner;
public class Lab4 {
// Create a method to solve the numbers factorial.
public static long myFactorial(long n) {
if (n == 0) {
return 1;
} else {
return (n * myFactorial(n - 1));
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long fact;
String choice = "y";
// Create a while loop to check if the user wants to continue or not
while (choice.equalsIgnoreCase("y")) {
// Ask for the users input
System.out.print("Please enter a number between 1 and 10" + '\n' + "Enter: ");
long num = input.nextLong();
input.nextLine();
// Call the method factorial
fact = myFactorial(num);
System.out.println("Factorial of " + num + " is: " + fact);
System.out.print("Would you like to continue y/n? " + '\n' + "Enter y or n: ");
choice = input.nextLine();
}
System.out.print("Program terminated");
input.close();
}
}