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();
        
       
       
       
        }
     }   
 }

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Can you explain your problem? Does it compile? Does it execute?
Do you get errors? Please post them here.
Show what the program outputs and add comments to it to show what the output should be.

Edited 4 Years Ago by NormR1: n/a

This is what it outputs i cant seem to get it right. Im doing something wrong. :/
it should be the factorial of 5 is: 120

Welcome
This program calculates factorials for numbers 1-10.
Enter an integer 1-10:
5

The factorial of 5 is:3628800
Continue? (y/n):n

Edited 4 Years Ago by brandon66: n/a

What output do you get for other input values?

First you need to understand what the code steps are to do the task.
Can you make a list of the steps the program is to do?
Start with:
Ask user for number
get number from user
???? What next?

Explain what the loop is used for and how many times it is supposed to execute.

I get the same output value each time i run the application. I get 3628800; s the factorial of numbers 1-10.

Step1: use while loop to see if the user wants to contiunue
Step2: Ask user for integer
Step3: Use the for loop to calculate the factorials
Step4:Use the if statement to make sure integer is <= 10 but greater than 0
Step5: asks if user want to continue if no is selected program is done.

Edited 4 Years Ago by brandon66: n/a

Ok now break this step up into its parts
Step3: Use the for loop to calculate the factorials

What output do you get for other input values?

This is the output for values greater than ten.

Welcome
This program calculates factorials for numbers 1-10.
Enter an integer 1-10:
12
Your number is out of range.
The factorial of 12 is:3628800
Continue? (y/n):n

^if the value is greater than ten, the output should be
Your number is out of range.
Continue? (y/n):


Step3: Use the for loop to calculate the factorials

//use the for loop
     for(i = 1; i <= 10; i++)

I get the same output value each time i run the application. I get 3628800; s the factorial of numbers 1-10.

Step1: use while loop to see if the user wants to contiunue
Step2: Ask user for integer
Step3: Use the for loop to calculate the factorials
Step4:Use the if statement to make sure integer is <= 10 but greater than 0
Step5: asks if user want to continue if no is selected program is done.

lets see the logic of getting a factorial:

1) we have a number say 4
2) we now multiple that number like this 4*3*2*1=factorial
3) lets get an equation to work out what number we begin multiplying with
number-1. number-1 would be the number we begin multiplying with
4) now take that logic and use a for statement. what number do we want to begin at? number-1. from there what do we want?
5) We want to decrease the number we start multiplying with by 1 so we will use '--'.
6) and what is the last constraint the number we multiply never gets to zero.

using that you can see this appearing:

int number = 4;
        for (int n = number - 1; n > 0; n--) {
            System.out.println(n);//see whats the value of n through the loop
}

Edited 4 Years Ago by DavidKroukamp: n/a

You've made a simple logic mistake Brandon! Your for loop is set to always loop 10 times (thus the factorial of 10, not your input). Perhaps this would help?

int result = 1;
for(int i = 1; i <= num; i++)
	result *=i;

What output do you get for other input values?
What about 2 or 3 or 6 or 8?
Is the answer always the same?

Thank you everyone! :) I was able tocode the app and learn what i did wrong from your help. Thank you!

Here is the final code i ended up with which gives the factorials for numbers 1-10 and if there is a number greater than ten say 12 it will say invalid number.

package factorial;

/**
 *
 * @author Brandon McBride
 */
import java.util.Scanner;
public class Factorial {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     // Welcome statement
     System.out.println
    ("Welcome \nThis program calculates factorials for numbers 1-10.");
     
     //new scanner
     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
      System.out.println("Enter an integer 1-10:  ");
      int num = sc.nextInt();
      //ifthe number is >= 1 execute for loop if not execute else statement
      if(num >= 1 && num <= 10)
       {
       int result = 1;
       for(int i = 1; i <= num; i++)
           result *= i;
       
       System.out.println("The factorial of " + num + " is:" + result);  
       }
        else
       {
           System.out.println("Your number is out of range."  );
           
       }
        
    //see if user wants to continue
       System.out.print("Continue? (y/n):");
       choice = sc.next();
       System.out.println();
    
    
    
        }
    }
}

Edited 4 Years Ago by brandon66: n/a

This question has already been answered. Start a new discussion instead.