// 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);
int factorial = 1;
int input = 0;
System.out.print("Enter a number between 1 and 10 (Enter 0 to exit): ");
input = scan.nextInt();
while (input != 0) {
if (input >= 1 && input <= 10) {
for (int ii = factorial; ii <= input; ii++) { // for loop - fixed - changed code
factorial *= ii;
}
System.out.println("\n" + input + "! = " + factorial);
factorial = 1; // reset factorial to 1 - fixed - added code
System.out.print("\nEnter a number between 1 and 10 (Enter 0 to exit): ");
input = scan.nextInt();
} else {
System.out.print("\nThe number you entered was not between 1 and 10.");
System.out.print("\nRe-enter a number between 1 and 10 (Enter 0 to exit): ");
input = scan.nextInt();
}
}
scan.close(); // close the scanner object - fixed - added code
System.out.println("\nGoodbye!");
}
}