Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Factorial program

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Factorial program

    Hello everyone.

    I'm working on some exercises and I'm having some problems with a method. I want to create a method to calculate the Factorial of an int number. I already wrote code that asks the user to input an int number and it calculates the Factorial, and it works fine (ie: if I input 5 it outputs
    5! = 120
    , as it should. Here's the code:

    import java.util.Scanner;
     
    public class Factorial1
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            int number;
            int total = 1;
     
            System.out.print("Enter positive int number: ");
                number = input.nextInt();
     
            while(number < 0)
            {
                System.out.print("Enter positive int number: ");
                number = input.nextInt();
            }
     
            for(int i = number; i > 0; i--)
                total *= i;
     
            System.out.printf("%d! = %d", number, total);
        }
    }

    Now I want to make a method to re-use this code in other programs and I wrote this program:

    public class TestClass
    {
        public static void main(String[] args)
        {
            System.out.print(factorial(5));
        }
     
        public static int factorial(int x)
        {
            int total = 0;
     
            for(int i = x; i > 0; i--)
                total *= i;
     
            return total;
        }
    }

    But when I run this program it outputs 0 instead of 120. I don't have the slightest idea of what is wrong with this code as it compiles just fine but doesn't work as intended.

    Any help will be very appreciated, and thanks in advance!

    Ricardo
    Last edited by rchirino; March 2nd, 2014 at 11:51 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Factorial program

    it outputs 0
    What is the value of total when
    total *= i;
    is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    rchirino (March 2nd, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Factorial program

    Oh... Fixed. Thanks Norm. Gotta pay more attention.

Similar Threads

  1. Factorial Test Program
    By phendrickson in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 23rd, 2014, 11:55 AM
  2. Factorial of a Number (program completed but doubt in that)
    By arunjava in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 16th, 2013, 04:26 AM
  3. Factorial loop help
    By iamgonge in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 20th, 2013, 07:07 PM
  4. Having problem with factorial
    By wickedsunday in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2012, 02:44 PM
  5. [SOLVED] Factorial of 1-20
    By pitchblack in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2011, 01:33 PM