-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prog162a.java
100 lines (77 loc) · 2.44 KB
/
Prog162a.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Prog 162a
* Bianca Smart
* 10/11/16
* BlueJ
* This program takes an inputted number and finds the factorial
*
* Learned - N/a
* Difficulties - Trying to find the simplest way to toop the entire program
*
**/
import java.util.*;
public class SmallFactorials
{
static Scanner kb = new Scanner(System.in);
public static void main(String[] args)
{
int n,end,sum = 0; //Declares ints
boolean check = true; //Declares boolean to see if user wants to continue
do{
System.out.print("Enter a Number: "); //Runs the factorial calculation
n = kb.nextInt();
end = n; //Declaring variables
sum = 1;
for(int x = n; x>0; x--) //Calculates factorial with for loop
{
sum*=x;
}
System.out.println("The value of "+n+"! is "+sum); //Prints output
System.out.print("\nWould you like to calculate another number? ");
String answer = kb.next(); //Gets user's response
if(answer.equals("Yes")) //Checks if user wants to continue
{
System.out.print("\n");
continue;
}
else //If they want to terminate the program
{
check = false; //Makes boolean false
System.out.print("\n\"End Factorial Processing\"");
}
}
while(check == true); //Continues if user wants to enter another number
}
}
/**
* Output:
* Enter a Number: 6
The value of 6! is 720
Would you like to calculate another number? Yes
Enter a Number: 9
The value of 9! is 362880
Would you like to calculate another number? Yes
Enter a Number: 12
The value of 12! is 479001600
Would you like to calculate another number? No
"End Factorial Processing"
Enter a Number: 1
The value of 1! is 1
Would you like to calculate another number? Yes
Enter a Number: 2
The value of 2! is 2
Would you like to calculate another number? Yes
Enter a Number: 3
The value of 3! is 6
Would you like to calculate another number? Yes
Enter a Number: 4
The value of 4! is 24
Would you like to calculate another number? Yes
Enter a Number: 5
The value of 5! is 120
Would you like to calculate another number? Yes
Enter a Number: 6
The value of 6! is 720
Would you like to calculate another number? Nope
"End Factorial Processing"
*/