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 8 of 8

Thread: Factorial loop help

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Factorial loop help

    Ok so I need to find the smallest integer whose factorial is greater than 2000

    current code:
    public class FactorialInt {
    public static void main (String args[]){

    int value = 7, factorial = 1, temp= value;

    while(temp > 0){
    factorial *=temp;
    temp--;
    }
    System.out.println("The factorial of " + value + " is " + factorial + ".");
    }}


    not sure how to change this code to solve problem. I can find the factorial for a int that i set but i how do i have it find the int that meets the requirements?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Factorial loop help

    how do i have it find the int that meets the requirements?
    Brute force? Start with 3 and go from there until you've found the int that meets the requirements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Factorial loop help

    i did that and its 7, bit the program is to i guess run a loop that finds that number

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Factorial loop help

    The loop needs to keep changing the number until the desired results are found.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Factorial loop help

    not sure how to make that happen since i have a loop going already, do i add another loop or does my current loop need to be changed?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Factorial loop help

    Have a loop that starts at 1 and changes the factor until the desired result.
    Save the last factorial and use for next computation.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Factorial loop help

    public class FactorialInt {

    public static void main (String args[]){
    int input_number = 0;
    int factorial = 1;

    while (factorial < 2000){
    input_number = input_number + 1;
    factorial = factorial * input_number;
    }
    System.out.println( "The Factorial of " + input_number+ " is " + factorial );
    }
    }


    this seems to work please check!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Factorial loop help

    this seems to work
    If you get the correct results, then that solves it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Factorial calculation optimization
    By aesguitar in forum Algorithms & Recursion
    Replies: 5
    Last Post: February 24th, 2013, 03:00 AM
  2. Having problem with factorial
    By wickedsunday in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2012, 02:44 PM
  3. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  4. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 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