FACTORIAL PROGRAM

U

Umesh

i wrote the following program to calculate factorial:

#include<stdio.h>
#include<iostream.h>
void main()
{
int i,n;
long int p=1; // or long double p=1; for exponential result which I
don't want.
cout<<"Enter No. ";
cin>>n;
if(n==0)
{
cout<<"1" ;
}
else
{
for(i=n;i>=1;i--)
{
p=p*i ;
}
}
cout<<"Factorial = "<<p;
}

But it works upto factorial 12 to display result as an integer. How
can I improve it so that it can display the result as an INTEGER upto
factorial 100 or more?.

THANK YOU.
 
G

Gianni Mariani

Umesh wrote:
....
But it works upto factorial 12 to display result as an integer. How
can I improve it so that it can display the result as an INTEGER upto
factorial 100 or more?.

You will need to use a Multiple Precision Arithmetic Library.

http://gmplib.org/
 
C

Chris Dollin

Umesh said:
i wrote the following program to calculate factorial:

#include<stdio.h>
#include<iostream.h>

That's not standard C.
void main()

That's a well-know not-Standard DON'T DO THAT for C.
{
int i,n;
long int p=1; // or long double p=1; for exponential result which I
don't want.

That's a syntax error illustrating why // comments are Bad News, Quillan,
in usenet postings.
cout<<"Enter No. ";

And that's either an attempt to shift an undeclared integer variable
by a char* amount, which is illegal in C for at least two reasons,
or C++, meaning that you misread the map and turned right, instead
of left, at the last crosscorridors.
But it works upto factorial 12 to display result as an integer. How
can I improve it so that it can display the result as an INTEGER upto
factorial 100 or more?.

Use an integer type that can cope with bigger numbers, such as supplied
by an off-topic-here bignum package.

--
Is it a bird? It is a plane? No, it's: http://hpl.hp.com/conferences/juc2007/
There' no hortage of vowel on Uenet.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
R

Richard Heathfield

Umesh said:
i wrote the following program to calculate factorial:

#include<stdio.h>
#include<iostream.h>

No such header, in either C or C++.
void main()

Incorrect declarator for main, in either C or C++.
{
int i,n;
long int p=1; // or long double p=1; for exponential result which I
don't want.

'don't want.' is a syntax error, in either C or C++.
cout<<"Enter No. ";

If you don't take steps to ensure otherwise, cout is undefined - in both
C and C++.

Same for cin.
if(n==0)
{
cout<<"1" ;
}
else
{
for(i=n;i>=1;i--)
{
p=p*i ;
}
}
cout<<"Factorial = "<<p;
}

But it works upto factorial 12 to display result as an integer.

Really? It won't compile here. Unterminated character constant.
How
can I improve it so that it can display the result as an INTEGER upto
factorial 100 or more?.

#include <stdio.h>

int main(void)
{
puts("100! =");
printf("9332621544394415268169923885626670049071596826438162");
printf("14685929638952175999932299156089414639761565182862536");
printf("97920827223758251185210916864000000000000000000000000\n");
return 0;
}

Writing your own routines for handling very large integers, or even
using someone else's, can be quite a tricky task for a beginner. I
suggest you focus on re-learning the basics, properly this time, and
save your integer adventures for later.
 
M

Martin Ambuhl

Umesh said:
i wrote the following program to calculate factorial:

#include<stdio.h>
#include<iostream.h>
^^^^^^^^^^^
not a C or C++ header
void main()
^^^^
not legal in C or C++
{
int i,n;
long int p=1; // or long double p=1; for exponential result which I
don't want.
cout<<"Enter No. ";
^^^^1 ^^^^^^^^^^^^2
1)Use of an undefined variable in C.
2)Illegal operand for the shift operator in C
and the result of the shift is just thrown away.
Your post certainly does not belong in comp.lang.c
(and it isn't really C++ either)

[...]
But it works upto factorial 12 to display result as an integer. How
can I improve it so that it can display the result as an INTEGER upto
factorial 100 or more?.

100! takes 532 bits. Until you buy a computer with an integer size >=
532 bits with a compiler that supports it, find one of the many extended
precision or "bignum" packages around and waste your time learning to
use it. It will be a waste because you need to spend that time learning
the programming language your are trying to use. That language appears
to be C++, but your program isn't.
 
U

Umesh

#include<stdio.h>
#include<iostream.h>
void main()
{
int n;
long double fac(int);
cout<<"Enter No. ";
cin>>n;
cout<<"Factorial = "<<fac(n);
}

long double fac(int n)
{
if(n<=1)
return(1);
else
return(n*fac(n-1));
}
 
G

Gunvant Patil

#include<stdio.h>
#include<iostream.h>
void main()
{
int n;
long double fac(int);
cout<<"Enter No. ";
cin>>n;
cout<<"Factorial = "<<fac(n);

}

long double fac(int n)
{
if(n<=1)
return(1);
else
return(n*fac(n-1));



}- Hide quoted text -

- Show quoted text -

If you don't care to read the replies to your post and go on making
same mistakes again why are you wasting yours and ours time?


-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.
 
J

James Kanze

In what language? The code is cross-posted to both comp.lang.c
and comp.lang.c++. It's legal in neither C nor C++, but in
order to indicate what's right, we have to know which language
you want to use.
#include<stdio.h>

Typically, C. But you don't seem to use anything from it, so
there's no reason to include it.
#include<iostream.h>

Out of date C++. To be used only when portability to a (very)
old compiler is necessary.
void main()

Illegal in C++, requiring a diagnostic from the compiler.
Implementation defined (but possibly illegal) in C.
{
int n;
long double fac(int);

I don't think many people will approve of a function declaration
inside another function. Formally, it's legal, but more for
historical reasons than anything else.

Some C++ compilers may warn, because in C++, what one might
write intuitively as a data definition ends up being interpreted
as a function declaration.
cout<<"Enter No. ";
cin>>n;
cout<<"Factorial = "<<fac(n);

There's no cout nor cin defined in either C or C++.
long double fac(int n)
{
if(n<=1)
return(1);
else
return(n*fac(n-1));
}

Interesting, but it doesn't give the correct results for 100!
either.

As others have pointed out, you need an integer with more
precision. Such things exist, and aren't that hard to use in
C++ (but not as part of the standard language), IF you know the
language.
 
C

CBFalconer

Umesh said:
i wrote the following program to calculate factorial:

#include<stdio.h>
#include<iostream.h>
void main()
{
int i,n;
long int p=1; // or long double p=1; for exponential result which I
don't want.
cout<<"Enter No. ";
cin>>n;

C++ is another language. Not to be found on c.l.c.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
P

per9000

i wrote the following program to calculate factorial:

But it works upto factorial 12 to display result as an integer...

<snip>

As you have been told there is an upper limit for how large integers
can be. Experiment with your factorial programs with f.x. short int,
unsigned short int, long, unsigned long, float and double (if you
don't know what that is search the internet for an explanation). The
results might learn you something - or at least give you funny output.

*Off-list-warning*
If you *absolutely* want to use integers in a factorial program - try
a programming language like Python.

Here is a minimal example (with no error checks or anything) that
works for me. "fac" computes the factorial of a number, then I print
the factorials for 0, 10, 20, ... 100.

code:
def fac(n):
for i in range(1,n):
n *= i
return n

for i in range(0,101,10):
print i, fac(i)

output:
0 0
10 3628800
20 2432902008176640000

<snip>

100
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

HTH,
Per
 
P

per9000

If you *absolutely* want to use integers in a factorial program - try
a programming language like Python.

<snip>

Or as Gianni said: use built-on-packages to extend the range of
"integers" to stay in C. You are interested in staying in C for
reasons known to readers on this list: "if C and Python were two WWF-
wrestlers then C would beat the crap out of python (if your C program
would pass compilation, and if you did not upset Malloc, and if you
did not misuse the & and/or *, etc.)".
[:)]-|--<

/Per
 
O

osmium

Umesh said:
i wrote the following program to calculate factorial:
But it works upto factorial 12 to display result as an integer. How
can I improve it so that it can display the result as an INTEGER upto
factorial 100 or more?.

You've had several suggestions, and which one is best depends on what your
goal is. If your goal is to become a better programmer, one way is to write
your own routines using char arrays as the fundamental element. Note that
multiplication is repeated addition. Consider unsigned numbers only. For
addition, simulate grade school arithmetic; note that the result of adding
two digits is:

c = a + b + ci
if(c>9)
c = c-10
ci=1 // ci to next digit
else
ci = 0

where
o prodce c = a+b
o a, b and c are individual digits of the larger underlying numbers
o ci stands for carry in and is 0 for the rightmost digit
o using the bastardized C++ meaning for '='.

There may be errors. When you finish you should have a good grasp of the
distinction between binary numbers and character codes. As in ASCII.

If you use arrays instead of vectors, use Stirling's (aka Sterling)
approximation and (porbably) logarithms to determine how many digits are in
the result - so you can size the arrays.

This link may be hellpful.

http://en.wikipedia.org/wiki/Binary-coded_decimal
 
H

Howard

'don't want.' is a syntax error, in either C or C++.

Ever heard of word wrapping? That's pretty obviously part of the comment on
the line above. If it was a syntax error, do you think it would have
compiled in the first place?

-Howard
 
A

Army1987

Howard said:
Ever heard of word wrapping? That's pretty obviously part of the comment
on the line above. If it was a syntax error, do you think it would have
compiled in the first place?

That's why you'd better avoid // comments on Usenet.
 
H

Howard

Army1987 said:
That's why you'd better avoid // comments on Usenet.

So a person's code shouldn't contain //-style comments, because someday you
might want to copy&paste it into a newsreader? Hmm...

-Howard
 
A

Army1987

Howard said:
So a person's code shouldn't contain //-style comments, because someday
you might want to copy&paste it into a newsreader? Hmm...

Re-read the last two words of my post.
You are certainly free to use // comments (if you have a fully C99
conforming compiler, which I don't believe to be te case, or if you don't
care about not completely conforming any current or former standard, the
latter case being OT here), and to replace them with /* */ comments (or to
leave them unchanged, if they are *very* short, so that the lines which
contain them won't become longer than 67 characters even when preceded by
umpteen > quotation marks).
 
G

Gregor H.

So a person's code shouldn't contain //-style comments, because someday you
might want to copy&paste it into a newsreader? Hmm...
Right. What a powerful "argument" against using //-comments in r e a l
code... ;-)


G.
 
R

red floyd

Gregor said:
Right. What a powerful "argument" against using //-comments in r e a l
code... ;-)

Note that this is crossposted to comp.lang.c as well, and C89 does not
recognize // comments.
 
H

Howard

red floyd said:
Note that this is crossposted to comp.lang.c as well, and C89 does not
recognize // comments.

Ahh, I hadn't noticed that. I just assumed we were talking C++ here.
-H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top