Results 1 to 9 of 9

Thread: [RESOLVED] different answer

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Resolved [RESOLVED] different answer

    Hello

    i was tring to figure out what the result of factorial for 21 using the following code.

    java code

    Code:
    import java.io.*;
    /**
     *
     * @author  
     */
    public class mywelcome {
      public static void main (String[] args) throws IOException
      {
        BufferedReader userin = new BufferedReader 
            (new InputStreamReader(System.in));
        String inputData;
        long    N, fact = 1; 
    
        System.out.println( "Enter N:" );
        inputData = userin.readLine();
        N         = Integer.parseInt( inputData );
    
        if ( N >= 0 )
        {
          while ( N > 1 )    
          {
            fact = fact * N;
            N    = N - 1;
          }
          System.out.println( "factorial is " + fact );
        }
        else
        {
          System.out.println("N must be zero or greater");
        }
      }
    }
    when i use the code in the netbeans environment, i get the answer -4249290049419214848
    but the following link states the answer should be 51090942171709440000

    http://chortle.ccsu.edu/CS151/Notes/...r+greater&N=20

    can you guys tell me why the difference?
    Last edited by vb_student; Jun 23rd, 2006 at 06:05 PM.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: different answer

    I don't agree with what how you're doing it. Simply use a for loop:

    Code:
    long total = 1;
    for (int index=21; index >=1; --index)
    {
      total *=index;
    }
    Something along those lines... You can use recursion, but I'd prefer this method.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: different answer

    That's JavaScript, it's totally different than java besides "51090942171709440000" is very big to contain in a "long" variable,
    try this:
    Code:
    import java.io.* ;
    
    /**
     *
     * @author  Moyeen
     */
    public class My
    {
      public static void main (String[] args) throws IOException {
        BufferedReader userin = new BufferedReader(new InputStreamReader(System.in)) ;
        String inputData ;
        int N; 
        double fact = 1 ;
    
        System.out.println("Enter N:") ;
        inputData = userin.readLine() ;
        N = Integer.parseInt(inputData) ;
    
        if (N >= 0) {
          while (N > 1) {
            fact = fact * N ;
            N-- ;
          }
          System.out.println("factorial is " + fact) ;
        }
        else {
          System.out.println("N must be zero or greater") ;
        }
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: different answer

    thanks for the replies

    i have just started on java, and am going through this tutorial on central conneticut state university's web tutorial on java

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: different answer

    Good, welcome to java
    if you are done, please mark the thread resolved
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: different answer

    done. thanks again

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: different answer

    this is not how you mark a thread resolved!
    Click on "Thread Tools" and select "Mark Thread Resolved"
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: different answer

    double is potentially inaccurate. Use java.math.BigInteger instead. Note that you have to use the class's member functions instead of normal mathematical operators, though.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: [RESOLVED] different answer

    done

Posting Permissions

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



Click Here to Expand Forum to Full Width