factorial program.

I

ishwar mehta

/* HEY FRND HELP ME TO FIGURE OUT WRONG WITH THIS PROGRAM */
#include<stdio.h>
int main()
{
int i,n,x;
x=1;
printf("PUT THE NO.");
scanf("%d",&n);
if(n==0)
printf("THE FACTORIAL VALUE IS %d\n",x);
else
{
for (i=1;i<=n;i++)
x=x*i;
}
printf(" the value is %d\n",x);
return 0;
}

hey frnd i wrote this program to calculate the factorial value for a
number,but in the output i got wrong value for n>16.
my output for various values is as:
PUT THE NO.17
the value is -288522240
PUT THE NO.37
the value is 0
and so on...
PLZ help me...to correct the error.. if any..
 
B

Barry Schwarz

/* HEY FRND HELP ME TO FIGURE OUT WRONG WITH THIS PROGRAM */
#include<stdio.h>
int main()
{
int i,n,x;
x=1;
printf("PUT THE NO.");
scanf("%d",&n);
if(n==0)
printf("THE FACTORIAL VALUE IS %d\n",x);
else
{
for (i=1;i<=n;i++)
x=x*i;
}
printf(" the value is %d\n",x);
return 0;
}

hey frnd i wrote this program to calculate the factorial value for a
number,but in the output i got wrong value for n>16.
my output for various values is as:
PUT THE NO.17
the value is -288522240
PUT THE NO.37
the value is 0
and so on...
PLZ help me...to correct the error.. if any..

What is the largest value an int can have on your system? At what
point does n! (factorial n) exceed this value?

You should get in the habit of indenting your code consistently (using
spaces, not tabs). Once you start writing more complex functions, it
will make your code much easier to read and debug. Horizontal white
space also helps make code readable. For example:

if(n == 0)
printf("THE FACTORIAL VALUE IS %d\n", x);
else
{
for(i = 1; i <= n; i++)
x = x * i;
}
 
B

BartC

hey frnd i wrote this program to calculate the factorial value for a
number,but in the output i got wrong value for n>16.
my output for various values is as:
PUT THE NO.17
the value is -288522240
PUT THE NO.37
the value is 0
and so on...
PLZ help me...to correct the error.. if any..

Try it like this:

#include<stdio.h>
int main(void)
{
int i,n,x;

for (n=0; n<20; ++n) {
{
x=1;
for (i=1;i<=n;i++)
x=x*i;
}
printf("%d! is %d\n",n,x);
}
}

On my machine, it starts giving the wrong answers from 13! onwards, but only
goes negative a bit later.

In fact, 32-bits allows up to 12!, 64-bits up to 20! I think.
 
B

Bl0ckeduser

/* HEY FRND HELP ME TO FIGURE OUT WRONG WITH THIS PROGRAM */
#include<stdio.h>
int main()
{
int i,n,x;
x=1;
printf("PUT THE NO.");
scanf("%d",&n);
if(n==0)
printf("THE FACTORIAL VALUE IS %d\n",x);
else
{
for (i=1;i<=n;i++)
x=x*i;
}
printf(" the value is %d\n",x);
return 0;
}

hey frnd i wrote this program to calculate the factorial value for a
number,but in the output i got wrong value for n>16.
my output for various values is as:
PUT THE NO.17
the value is -288522240
PUT THE NO.37
the value is 0
and so on...
PLZ help me...to correct the error.. if any..


Perhaps writing custom "big number" code would constitute a fruitful
programming and learning experience.

This is how I'd do it (beware, I'm not an expert):

####################################################################

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DIG 1000000 /* maximum number of digits */

void bignum_add(char*, char*);
void bignum_put(char*);

int main(int argc, char** argv)
{
char bignum_1[DIG] = {0};
char bignum_2[DIG] = {0};

int i, j = 0, k = 0;

bignum_1[DIG - 1] = 1;

if(argc < 2){
printf("use: %s n\n", argv[0]);
exit(0);
}
sscanf(argv[1], "%d", &i);

while(i--){
memcpy(bignum_2, bignum_1, DIG);
for(j = i; j>0; j--)
bignum_add(bignum_1, bignum_2);
}

bignum_put(bignum_1);
return 0;
}

void bignum_put(char* a){
int i = 0;
while(!a) ++i;
while(i < DIG) putchar(a[i++] + '0');
putchar('\n');
}

void bignum_add(char* a, char* b){
char carry[DIG];
int i = DIG;
int x = 0;

carry = 0;

while(i >= 0){
carry[i - 1] = 0;
a += b;
a += carry;
if(a > 9){
if(i > 0){
carry[i - 1] = a / 10;
a -= (char)(a / 10) * 10;
} else {
printf("integer overflow\n");
exit(0);
}
}
i--;
}
}
####################################################################

$ ./a.out 3
6
$ ./a.out 17
355687428096000
$ ./a.out 30
265252859812191058636308480000000
$ ./a.out 50
30414093201713378043612608166064768844377641568960512000000000000
$
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top