Search code examples
cpointersheaderlong-integerfactorial

Why the factorial of 153 has failed in following program


I have created a myfun.h header file with two functions in it. A factorial function and amstrong function and a myfun.c file Here is myfun.h program

void factorial(int n,int *fact)
{
    int i;
    *fact=1;
    for(i=1;i<=n;i++)
      *fact=*fact*i;
}
amstrong(int n)
{
    int sum=0,num,rem,cube;         
    num=n;
    while(num>0)
    { 
      rem=num%10;
      cube=rem*rem*rem;        
      num=num/10;
      sum=sum+cube;
    }
    if(sum==n)                         
      return(1);  
    else
      return(0);
 }

Here is the myfun.c program

#include<stdio.h>
#include "myfun.h"
int main()
{
    int num,rev,f,code;
    printf("Enter number :");
    scanf("%d",&num);
    code=amstrong(num);
    if(code==1)
      printf("\nNumber is amstrong\n");  
    else
      printf("Number is not amstrong\n");
    factorial(num,&f);
    printf("Factorial of %d is %d ",num,f);
    getch();
}

In this the amstrong function is working fine.But the factorial function is giving output 0. I haven't tried it without removing pointer variable. But if i want to run it with pointer variable then what changes i need to do?

The output of program is

Enter number: 153
Number is amstrong
Factorial of 153 is 0

Solution

  • 153! = 2.01 E+269.

    In case unsigned long long is 64 bits, it can hold a maximum value of 2^64 = 18.45 E+19.

    You will need to use some form of "big int" library to calculate huge numbers like these.