1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Factorials

Discussion in 'Software Development' started by Gor, Sep 15, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. Gor

    Gor Thread Starter

    Joined:
    Sep 15, 2003
    Messages:
    4
    I am beginner in C++, currently trying to figure out how to print factorials on Unix machine in C++.
    Trying to print 1-50.
    The Point that I got to is 20th factorial using Long Int variable.
    Anyway to print factorials 1-50 in integer form, excluding any other notations (such as scientific) ?

    can anyone help out or suggest any clues?

    Thnx,

    Gor.
     
  2. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Have you tried, converting to a string, getting rid of the decimal point and the e+xx and using a loop to print the proper amount of zeros?

    You can store the length of everything after the decimal point (minus e+xx) and subtract that from the power to figure out how many zeros need to be printed after the first part.

    I don't have an example in c++, but do in javascript.
     
  3. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Here's the
    javascript example

    If you input 1-21, it will just display the number because notation is not required. 22 on, notation is required and it will display the result with notation and without.

    You can remove the if (i ==x) check, to display all results up to the number you input.

    I only tested in opera and IE. However opera displays the incorrect answer sometimes because it rounds up the last digit before the e+.

    It should give you an idea of a way you can accomplish what you want in c++.

    It's far from perfect, but that should help.

    Not for my sake, but someone should simplify that a whole lot and do it the proper way for javascript and c++.
     
  4. Gor

    Gor Thread Starter

    Joined:
    Sep 15, 2003
    Messages:
    4
    thnx very much for your help, I am gonna try to do the expample you suggested at first of converting to a string.
    Will reply here if I succeeded.

    Gor.
     
  5. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
  6. Gor

    Gor Thread Starter

    Joined:
    Sep 15, 2003
    Messages:
    4
    thank you that code really helps, I can base my own implementation design on it then.

    but one more thing, when I tried to test the code to see how it runs I get following error:
    --------------------------------------------------------------------------
    CLargeNumber::CLargeNumber[in-charge](int)
    CLargeNumber::eek:perator*=(CLargeNumber const&)
    CLargeNumber::eek:perator-=(CLargeNumber const&)
    CLargeNumber::ToString() const
    collect2: ld returned 1 exit status
    --------------------------------------------------------------------------

    perhaps my compiler can not handle it correctly,
    I tried debugging, still get the same problem.

    last message looks as if I have files named incorrectly, but I seem to have put the appropriate endings as in .C and .h as my compiler requires.

    Gor
     
  7. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Hmm. Not sure. Maybe if I see all of the code that you have so I can test it. If it's secret, then try www.hardforum.com and goto the web development and programming forum and ask.

    What compiler are you using?
    Are you using an IDE also?
     
  8. Gor

    Gor Thread Starter

    Joined:
    Sep 15, 2003
    Messages:
    4
    http://www.codeproject.com/cpp/largenumber.asp

    thats the code you refered me too, I d/led that .zip folder they had of the LargeNumbers class files.
    It contained LargeNumber.C LargeNumber.h and a test.C files,
    I didnt make any changed to them;
    tried to run their test file to see how the class works and got those errors listed above.

    Also I am using Unix based compiler, let me check name and version, yes IDE...
    Compaq Tru64 UNIX V5.1B (Rev. 2650)
    This machine is a Compaq Alpha ES40. It has two processors each running
    at 667MHz, and 2 Gig of error correcting, primary memory (RAM).
    Digital Unix

    Gor
     
  9. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    OK. I'll see if I can figure out what's wrong.

    Edit:

    I got a bunch of errors also. Not sure how to fix it. Try HardForums.
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/165063

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice