// Factorial (Loops) - Java- In Person Session
// Date: 2024-06-06
// Author: Steve Ellermann
// Note: This would result in incorrect output, however the student would figure out
// after testing it and come back with more questions or hopefully learned enough to
// move forward on their own
import java.util.Scanner;
public class FactorialUsingLoops {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int MIN = 1;
final int MAX = 10;
final int SENTINEL = 0;
int factorial = 1;
int input = 0;
System.out.print("Enter an integer between " + MIN + " and " + MAX + " inclusive " + "(Enter " + SENTINEL + " to exit): ");
input = scan.nextInt();
while (input != 0) {
if (input >= MIN && input <= MAX) {
for (int ii = factorial; ii <= input; ii--) {
factorial *= ii;
}
System.out.println("\n" + input + "! = " + factorial);
System.out.print("\nEnter an integer between " + MIN + " and " + MAX + " inclusive " + "(Enter " + SENTINEL + " to exit): ");
input = scan.nextInt();
} else {
System.out.println("\nThe integer you entered was not between " + MIN + " and " + MAX + " inclusive.");
System.out.print("Re-enter an integer between " + MIN + " and " + MAX + " inclusive " + "(Enter " + SENTINEL + " to exit): ");
input = scan.nextInt();
}
}
System.out.println("\nGoodbye!");
}
}