Thread: problem with large integers

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    18

    problem with large integers

    The small program below calculates factorials -but it works only up to 25. For input >=26 I get errors. How can I use bigger integers?

    Code:
    #include <stdio.h>
    int main()
    {
      int c, n ;
      long long factorial=1;
      printf("Enter a number to calculate it's factorial\n");
      scanf("%d", &n);
      for (c = 1; c <= n; c++)
        factorial = factorial * c;
      printf("Factorial of number %d = %lld\n", n, factorial);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need a library which supports arbitrary sized integers.
    https://gmplib.org/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accepting large integers as entry
    By Abhas in forum C Programming
    Replies: 13
    Last Post: 04-10-2011, 01:03 PM
  2. Large integers multiplications
    By ahmedBanihammad in forum C Programming
    Replies: 3
    Last Post: 03-27-2011, 12:23 PM
  3. Storing and printing large integers
    By newbie123 in forum C Programming
    Replies: 8
    Last Post: 09-16-2010, 01:17 PM
  4. Messing with Large Integers
    By johnnie2 in forum Windows Programming
    Replies: 3
    Last Post: 11-16-2002, 01:22 PM
  5. Storing large integers....why doesn't this work?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-21-2001, 09:41 PM