The while
loop doesn’t have curly braces, so the only thing in the loop’s body is the for
loop. num
starts at 1
, and nothing inside the loop ever increases it, so it would loop forever.
You don’t need a nested loop, though – a single loop that calculates the factorial as you go should suffice:
int numinput = 150; //number than we are trying to find the largest factorial less than
int num = 1;
int factorial = 1;
while (factorial < numinput) {
num++;
factorial *= num;
}
// We overshot to terminate the loop, go back one number
factorial /= num;
num--;
System.out.println
("The largest factorial less than " + numinput + " is " + num + "!, or " + factorial);
CLICK HERE to find out more related problems solutions.