This is what i am suppossed to do:
Create an application that will ask the user for a number between 1 and 10 and then output the factorial of that number. Use a for loop to calculate the factorial.
What is a factorial? A factorial is the product of all the numbers from 1 to the number specified. For instance, if the number is 4, then 4! (4 factorial) is 4 * 3 * 2 * 1 = 24. 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040. Since the output for the factorial grows quickly, we will limit the numbers from 1 to 10.
◦Use the while loop to allow the user to execute the program as many times as they want.
◦Use a for loop to calculate the factorial
◦Use an if statement to make sure that the number is less than or equal to 10 but greater than 0
◦Read the number in as an Integer
This is what i have. Can you help?
public static void main(String[] args) {
//Welcome staement
System.out.println
("Welcome \nThis program calculates factorials for numbers 1-10.");
Scanner sc = new Scanner (System.in);
//perform caculations until choice isn't equal to "y" or "Y"
String choice = "y";
while (choice.equalsIgnoreCase ("y") )
{
//get integer from user
int num = 1;
System.out.println("Enter an integer 1-10: ");
num = sc.nextInt();
//compare number to range
if(num >= 1 && num <= 10)
{
System.out.println();
}
else
{
System.out.println("Your number is out of range.");
}
int result = 1;
for(int i = 1; i <= 10; i++)
result *=i;
System.out.println("The factorial of " + num + " is:" + result);
//see if user wants to continue
System.out.print("Continue? (y/n):");
choice = sc.next();
System.out.println();
}
}
}