I'm doing a prime factorization code, and I'm trying to print "x" between each displayed prime factor of the user-inputted number. However, I can't seem to figure out how to make it so that an extra "x" does not appear after the last displayed prime factor.
I've tried concatenating mod after the "x" in the print statement, and I've tried thinking about using an If statement for (startInt % mod == 0), but I'm not too sure what the else statement would be for that.
System.out.println("Enter an integer to be factored:");
int startInt = userInput.nextInt();
if(startInt % 2 == 0 || startInt % 3 == 0 || startInt % 5 == 0 || startInt % 7 == 0 || startInt % 11 == 0 || startInt % 13 == 0)
{
for(int mod = 2; mod <= startInt; mod++)
{
while(startInt%mod == 0)
{
startInt /= mod;
System.out.print(mod+" x ");
}
}
}
else
{
System.out.println(startInt+" = "+startInt);
System.out.println(startInt+" is a prime number.");
}
I expect the output of 784784 to be 2 x 2 x 2 x 2 x 7 x 7 x 7 x 11 x 13 but the actual output is 2 x 2 x 2 x 2 x 7 x 7 x 7 x 11 x 13 x.