Results 1 to 7 of 7
  1. #1
    er1c550n20 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default Factorial console program

    hey guys i know all of can help me here. Program is about user enters a number at run time and output should give the factorial.
    thanx in advance.
    I feel error is really simple but couldnt sort it out.


    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    public class NumberFactorial
    {
    public static void main(String args[])
    {
    int number;
    try
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the number");
    number = Integer.parseInt(br.readLine());

    int factorial = number;
    for(int i =(number-1);i>1;i--)
    {
    factorial=factorial*i;
    }
    }
    catch(NumberFormatException ne)
    {
    System.out.println("Number Format Exception"+ne);
    System.exit(0);
    }
    catch(IOException ioe)
    {
    System.out.println("Input Exception Error"+ioe);
    System.exit(0);
    }
    System.out.println("Factorial of number is:" +factorial);
    }
    }

  2. #2
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    Java Code:
    System.out.println("Factorial of number is:" +factorial);
    just move this line a bit up into the try block of ur code.

    please use code tags.. when u post ur code.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Your formula is wrong: by definition 0! == 1.

    kind regards,

    Jos

  4. #4
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    Your formula is wrong: by definition 0! == 1.
    Java Code:
    try
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the number");
    number = Integer.parseInt(br.readLine());
    int factorial=1;
    while(number>0)
    {
    	factorial = factorial * number;
    	number--;
    }
    System.out.println("Factorial of number is:" +factorial);
    }
    ur try block must look something like this.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    How about a simple recursive method?
    Java Code:
    int factorial(int n) {
      if(n == 0) return 1;
      return factorial(n-1)*n;
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by m00nchile View Post
    How about a simple recursive method?
    Java Code:
    int factorial(int n) {
      if(n == 0) return 1;
      return factorial(n-1)*n;
    }
    Great; factorial(-1)

    kind regards,

    Jos

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    It was just a reccomendation, I know it should check for values that it can't calculate.

Similar Threads

  1. Replies: 4
    Last Post: 04-14-2010, 04:01 AM
  2. Using method to call Factorial
    By hydride in forum New To Java
    Replies: 2
    Last Post: 03-02-2010, 02:16 AM
  3. Ready To Program, Images in HSA Console?
    By habbah in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 03:50 AM
  4. Factorial
    By Anindo in forum New To Java
    Replies: 4
    Last Post: 07-28-2009, 09:46 PM
  5. [SOLVED] overflow when calculate factorial
    By ravinda in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 05:07 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
  •