# Factorials

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

#### Gor

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.

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.

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++.

#### Gor

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.

#### Gor

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:perator*=(CLargeNumber const&)
CLargeNumber: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

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?

#### Gor

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

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.

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

As Seen On