1

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
riya
  • 23
  • 1
  • 5

4 Answers4

3

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.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • the fact parameter is only an int, so this answer is meaningless in the context of the question – user3629249 Sep 04 '15 at 19:06
  • @user3629249 It is assumed that the reader understands that unsigned long long is the largest standard integer type available, yet I showed it is too small. Sizes of integers is usually addressed in chapter 1 of your average C beginner's book. – Lundin Sep 07 '15 at 06:25
2

This is happening because there is a every data type can hold upto a certain number. The reason that you are getting wrong answer is because the fact of 153 would be bigger then what an int variable can hold. It should work fine for smaller values.

Edit

To store larger numbers you can use long long int data type.

Haris
  • 12,120
  • 6
  • 43
  • 70
  • i didn't say it will work for `fact(153)`. *I said to store larger numbers*. – Haris Sep 03 '15 at 11:08
  • but his problem is with factorial of 153 – venki Sep 03 '15 at 11:08
  • i have tried using long unsigned int , but still its showing 0 output – riya Sep 03 '15 at 11:16
  • actually the largest factorial that can be stored in an int variable is only 12. 13 exceeds the size of a int variable (31bits+sign) 13 factorial is 6,227,020,800 (greater than 6gig) and 12 factorial is 479,001,600 (less than .5gig) and the max value in an int is 2gig – user3629249 Sep 04 '15 at 19:04
1

The minimum data type ranges you can use are:

short int and int: -32,767 to 32,767
unsigned short int and unsigned int: 0 to 65,535
long int: -2,147,483,647 to 2,147,483,647
unsigned long int: 0 to 4,294,967,295

int alone cannot store the value given by fact (153). Use smaller values for your case, otherwise change the type: use a long long int instead.

1

sizeof(long double) = 12bytes on a machine running 32-bit linux. The output can be printed by using %LE in printf. Output will be in exponential form.

EDIT

factorial(20) : 2432902008176640000 when using long long int This is maximum you can get with long long int type of variable. For greater ranges use long double.