2

Fortunately this program works fine to find the factorials of 1 to 12 but after 12 as 13, 14, 20 ..... output getting wrong and i also try to find the factorial of 40 and the output was 0. Failed to find the exact problem...

#include <stdio.h>

int main() {

   int user_input, tbl;

   printf("Enter any number: \t");
   scanf("%i", &user_input);

   tbl = user_input; 

   for(int i=2; i < user_input; i++) {
      tbl = tbl * i;
   }

    printf("Factorial of %i is %i", user_input, tbl);

}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
ForWebTech
  • 140
  • 1
  • 13
  • 5
    Did you check the max value an `int` can hold? – Sourav Ghosh Jan 19 '16 at 10:08
  • 4
    Possible duplicate of [C / Find 190! (factorial) in c programming](http://stackoverflow.com/questions/19744538/c-find-190-factorial-in-c-programming) – Paul Hankin Jan 19 '16 at 10:15
  • @forweb check out [Ridiculously Large Number Multiplication](https://gist.github.com/jargnar/3263916), it provider a way to multiply numbers as string, which gives you ability to multiply numbers with almost no limit. – tchelidze Jan 19 '16 at 10:17
  • http://stackoverflow.com/questions/28032886/calculate-the-sum-of-digits-in-100-factorial – BLUEPIXY Jan 19 '16 at 10:27

2 Answers2

8

You're getting an integer overflow. On most machines, int is 32 bit wide (and obviously signed). That means the biggest number it can represent is 2^31-1 which is 2147483648. 12! works as it's 479001600 (which is smaller than 2^31-1) but 13! is 6227020800. So 13! can't usually be represented in an int.

One option you have is to make i, user_input and tbl a bigger type for example a unsigned long long or a uint64_t (from #include <inttypes.h>). But these types will also have a maximum representable number.

If you actually need arbitrary precision in C, you might want to consider GMP (The GNU Multiple Precision Arithmetic Library).

Also please be aware that overflows of signed types have undefined behaviour in C.

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
  • thanks and is it possible to check integer length with in a program ? – ForWebTech Jan 19 '16 at 10:13
  • 1
    @forweb it's a compile time property. You can query the length of an a type in bytes using `sizeof`. For example `sizeof(int)` will most likely be `4` for you. `CHAR_BITS` tells you how many bits a byte has (almost always `8`). – Johannes Weiss Jan 19 '16 at 10:15
  • 3
    @forweb if you're interested in the maximum number an `int` can hold, `#include ` does have `INT_MAX` (which is probably 2147483647 == 2^32-1). – Johannes Weiss Jan 19 '16 at 10:19
  • @JohannesWeiß 2^31-1 – mch Jan 19 '16 at 10:20
  • @JohannesWeiß thanks, and can we use 'double' to increase the size ? – ForWebTech Jan 19 '16 at 10:23
  • 1
    No need to check `sizeof int`. Every C implementation has a `` that contains a set of macros which expand to maximum values for all numeric types, among others. For example, `INT_MAX` is the maximum value of the (implementation defined) `int` type. For the more recent fixed-width integral types there is also a set of macros in ``. – Peter Jan 19 '16 at 10:23
  • 1
    @forweb not really, double are floating number, so you won't get an integer – Guiroux Jan 19 '16 at 10:36
  • @Guiroux then how we can increase the size of a integer? – ForWebTech Jan 19 '16 at 10:40
  • 1
    @forweb well the answer actually told you, use **unsigned long long** – Guiroux Jan 19 '16 at 10:41
0
  • 8bit unsigned char Max:0xFF-->255 ->2^8 - 1 (char)2^7 -1
  • 16bit unsigned short Max:0xFFFF -->2^16 - 1(short)2^15 - 1
  • 32bit unsigned int Max:0xFFFFFFFF -->2^32 - 1(int)2^31 - 1
  • 64bit unsigned.....Max:... --> 2^64 - 1(long long)2^63 - 1
  • All have Range.This is not the number written on the paper.
liuyip
  • 95
  • 1
  • 2