I am an AP Computer Science student and I was wondering how to finish up my factorial code using for loops. Here is what I have so far:
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
int num;
int factorial = 1;
int i;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
num = input.nextInt();
for(i = 1; i <= num; i++)
{
factorial *= i;
}
System.out.println("!"+num+"="+factorial);
I tested it using eclipse and it worked for all integers until (and including) 12. When I entered 13, it gave me an incorrect number. Can someone explain to me why that is and how to rectify it?
Also, the assignment says I need to print out the numbers that I'm multiplying in addition to the answer (i.e. if num = 5, then the output is 5! = 5*4*3*2*1 = 120). Can someone point me in the right direction for that issue?