473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

The wrong with factorial function

Hi,I am a new C++ learner.The follow prgram produces a wrong
result,but I can't find the wrong.Can you help me?Thank you!

#include<stdio.h>

long factorial( long number);

int main()
{
int a;

for ( a = 0;a<10;a++ ){
printf("%2d! = %1d\n",a,factorial( a ));
}

return 0;
}

long factorial( long number )
{
if ( number <=1 ){
return 1;
}
else{
return ( number * factorial(number - 1));
}

the result as follows
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! =5040
8! = -25216
9! = -30336
Atentions,please.The result 8! and 9! are wrong.But Why?
Nov 2 '08 #1
3 2929
>Hi,I am a new C++ learner.

Then why are you posting in comp.lang.c?
Your program does look like C, though.
>The follow prgram produces a wrong
result,but I can't find the wrong.Can you help me?Thank you!
factorials of numbers tend to be large, so expect overflow. You'll
overflow a signed long at 13! and a signed int at 8!. What is the
value of INT_MAX in <limits.hon your system? 32767?
>#include<stdio.h>

long factorial( long number);

int main()
{
int a;

for ( a = 0;a<10;a++ ){
printf("%2d! = %1d\n",a,factorial( a ));
You are printing a long with a %d format, which is supposed to be
used for ints. Use %ld. This is likely to cause all sorts of
trouble. Here, it seems to have taken a piece of the (32-bit?)
result and printed it as a (16-bit?) int.
}

return 0;
}

long factorial( long number )
{
if ( number <=1 ){
return 1;
}
else{
return ( number * factorial(number - 1));
}

the result as follows
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! =5040
8! = -25216
9! = -30336
Atentions,please.The result 8! and 9! are wrong.But Why?

Nov 2 '08 #2
Blue sky said:
Hi,I am a new C++ learner.
This is comp.lang.c, not comp.lang.c++.

Fortunately, your question is actually about a C program!
The follow prgram produces a wrong
result,but I can't find the wrong.Can you help me?Thank you!
I already did so, yesterday, in alt.comp.lang.learn.c-c++. I suggest you go
read that reply.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 2 '08 #3
Blue sky wrote:
Hi,I am a new C++ learner.The follow prgram produces a wrong
result,but I can't find the wrong.Can you help me?Thank you!
[...]
Atentions,please.The result 8! and 9! are wrong.But Why?
Luckily, none of your code involves C++ rather than C. If it had,
then questions about it would not be topical here, since C++ is a
different language.

Note what happens with the following code on your machine and think
about what that might mean. You might also try the same code with
'signed' instead of 'unsigned' and SHRT_MAX, LONG_MAX, and LLONG_MAX
instead of USHRT_MAX, ULONG_MAX, and ULLONG_MAX. Notice what happens then.

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned int i, usok = 1, ulok = 1, ullok = 1;
unsigned short usfact = 1;
unsigned long ulfact = 1;
unsigned long long ullfact = 1;
printf("[Output]\n"
"The limiting values are implementation-specific.\n\n");
for (i = 1; ullok; i++) {
if (usok) {
if (USHRT_MAX / i < usfact) {
printf("%u! will overflow an unsigned short.\n"
"An unsigned short is at most %u.\n", i,
USHRT_MAX);
usok = 0;
}
else {
usfact *= i;
ulfact *= i;
ullfact *= i;
printf("%u! is %u (unsigned short)\n", i, usfact);
}
}
if (!usok && ulok) {
if (ULONG_MAX / i < ulfact) {
printf("%u! will overflow an unsigned long.\n"
"An unsigned long is at most %lu.\n", i,
ULONG_MAX);
ulok = 0;
}
else {
ulfact *= i;
ullfact *= i;
printf("%u! is %lu (unsigned long)\n", i, ulfact);
}
}
if (!ulok) {
if (ULLONG_MAX / i < ullfact) {
printf("%u! will overflow an unsigned long long.\n"
"An unsigned long long is at most %llu.\n", i,
ULLONG_MAX);
ullok = 0;
}
else {
ullfact *= i;
printf("%u! is %llu (unsigned long long)\n", i,
ullfact);
}
}

}
return 0;
}

[Output]
The limiting values are implementation-specific.

1! is 1 (unsigned short)
2! is 2 (unsigned short)
3! is 6 (unsigned short)
4! is 24 (unsigned short)
5! is 120 (unsigned short)
6! is 720 (unsigned short)
7! is 5040 (unsigned short)
8! is 40320 (unsigned short)
9! will overflow an unsigned short.
An unsigned short is at most 65535.
9! is 362880 (unsigned long)
10! is 3628800 (unsigned long)
11! is 39916800 (unsigned long)
12! is 479001600 (unsigned long)
13! will overflow an unsigned long.
An unsigned long is at most 4294967295.
13! is 6227020800 (unsigned long long)
14! is 87178291200 (unsigned long long)
15! is 1307674368000 (unsigned long long)
16! is 20922789888000 (unsigned long long)
17! is 355687428096000 (unsigned long long)
18! is 6402373705728000 (unsigned long long)
19! is 121645100408832000 (unsigned long long)
20! is 2432902008176640000 (unsigned long long)
21! will overflow an unsigned long long.
An unsigned long long is at most 18446744073709551615.

Nov 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Eduardo78 | last post by:
Ok, this is what i am tying to do: I want to create a rutine that takes a value from a text box. Lets say for example 5. and does the following 5*4*3*2*1 = 120 the rutine should be able to...
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
11
by: Martin Jørgensen | last post by:
Hi, Consider (factorial.cpp): #include <iostream> using namespace std; double R=3.2; /* not used, but R is static because it is a global variable (file scope) */
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
59
by: Umesh | last post by:
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...
3
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
by: Killer42 | last post by:
Here's a very simple function for VB (written in VB6) to calculate the factorial (!) of a number. Note that it is limited by the magnitude of the value which can be stored in the Long data type. (In...
0
kadghar
by: kadghar | last post by:
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type. Why is this? You can...
9
by: upyzl | last post by:
I want to calculate n!,and my C code is as follows: #include <stdio.h> #include <stdlib.h> int main() { int nonnega_int, counter; unsigned long result = 1;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.