Results 1 to 5 of 5
  1. #1
    SusieM726 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Factorial of a Number Code

    I'm a beginning JAVA student and need help. My code asks for input but then I can't get output. Can someone please tell me where I went wrong?

    import java.util.Scanner;

    class Factorial
    {
    public static void main(String args[])
    {
    int n = 0, c, fact = 1;

    System.out.println("Welcome to the Factorial Calculator");
    System.out.println("Enter an integer that's greater than 0 and less than 10: ");
    Scanner in = new Scanner(System.in);

    String choice = "y";
    while (choice.equalsIgnoreCase("y"))

    n = in.nextInt();

    if ( n < 0 )
    System.out.println("Number should be non-negative.");
    else
    {
    for ( c = 1 ; c <= n ; c++ )
    fact = fact*c;

    System.out.println("Factorial of "+n+" is = "+fact);
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Factorial of a Number Code

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Is the program stuck in a loop? Look at the loops in the program to see where it could be looping forever.

    HINT: If the value tested in the loop's condition expression is not changed inside the loop, the loop will go forever.

    You should ALWAYS use {}s to enclose the code inside loops and following if statements.
    Java Code:
      while(the condition) {
         some code here
       }  // end while
    Last edited by Norm; 11-28-2013 at 08:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SusieM726 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Factorial of a Number Code

    Sorry Norm - don't know how to edit that post.

    No it's not in a loop - there is no output.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Factorial of a Number Code

    Can you copy the contents of the command prompt window from when you execute the program and paste it here.

    Add the {}s I suggested and post the new version of the program. Be sure to wrap the code in code tags.

    No it's not in a loop
    Can you continue entering numbers to the program as it executes?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Factorial of a Number Code

    Part of the problem is your formatting and lack of {}. What code is subject to the while statement?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 10-28-2011, 08:18 AM
  2. JSP factorial of number and NumberFormatException
    By sandraW in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 10-12-2010, 07:46 AM
  3. Highest number code
    By raey_juve in forum Introductions
    Replies: 1
    Last Post: 05-24-2010, 06:42 AM
  4. Convert bar code to number
    By samatha_kudi in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2009, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •