Maximum value of an integer for which factorial can be calculated on a machine - GeeksforGeeks

26 Jun, 2021
Program to find maximum value of an integer for which factorial can be calculated on a machine, assuming that factorial is stored using basic data type like long long int.
The idea is based on that fact that, in most of the machines, when we cross limit of integer, the values becomes negative. 
C
Java
Python3
C#
PHP
Javascript
// C program to find maximum value of// an integer for which factorial can// be calculated on your system#include <stdio.h> int findMaxValue(){    int res = 2;    long long int fact = 2;    while (1)    {        // when fact crosses its size,        // it gives negative value        if (fact < 0)            break;        res++;        fact = fact * res;    }    return res - 1;} // Driver Codeint main(){    printf ("Maximum value of integer : %d\n",                                 findMaxValue());    return 0;}
// Java program to find maximum value of// an integer for which factorial can be// calculated on your systemimport java.io.*;import java.util.*; class GFG{    public static int findMaxValue()    {        int res = 2;        long fact = 2;        while (true)        {            // when fact crosses its size,            // it gives negative value            if (fact < 0)                break;            res++;            fact = fact * res;        }        return res - 1;    }     // Driver Code    public static void main(String[] args)    {        System.out.println("Maximum value of"+                                 " integer " +                              findMaxValue());    }}
# Python3 program to find maximum value of# an integer for which factorial can be# calculated on your systemimport sysdef findMaxValue():     res = 2;    fact = 2;    while (True):                 # when fact crosses its size        # it gives negative value        if (fact < 0 or fact > sys.maxsize):            break;        res += 1;        fact = fact * res;    return res - 1; # Driver Codeif __name__ == '__main__':         print("Maximum value of integer:",                      findMaxValue());     # This code is contributed by 29AjayKumar
// C# program to find maximum value of// an integer for which factorial can// be calculated on your systemusing System; class GFG{    public static int findMaxValue()    {        int res = 2;        long fact = 2;        while (true)        {            // when fact crosses its size,            // it gives negative value            if (fact < 0)                break;            res++;            fact = fact * res;        }        return res - 1;    }     // Driver Code    public static void Main()    {        Console.Write("Maximum value of"+                            " integer " +                         findMaxValue());    }} // This code is contributed by nitin mittal
<?php// PHP program to find maximum// value of an integer for which// factorial can be calculated// on your systemfunction findMaxValue(){    $res = 2;    $fact = 2;    $pos = -1;    while (true)    {        // when fact crosses its size,        // it gives negative value        $mystring = $fact;        $pos = strpos($mystring, 'E');             if ($pos > 0)            break;        $res++;        $fact = $fact * $res;    }    return $res - 1;} // Driver Codeecho "Maximum value of".           " integer " .         findMaxValue();     // This code is contributed by Sam007?>
<script> // Javascript program to find maximum// value of an integer for which factorial// can be calculated on your systemfunction findMaxValue(){    let res = 2;    let fact = 2;         while (true)    {                 // When fact crosses its size,        // it gives negative value        if (fact < 0 || fact > 9223372036854775807)            break;                     res++;        fact = fact * res;    }    return res - 1;} // Driver Codedocument.write("Maximum value of"+               " integer " + findMaxValue()); // This code is contributed by rag2127 </script>
Output : 
Maximum value of integer : 20
This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
Sam007
29AjayKumar
rag2127
factorial
Mathematical
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Program to multiply two matrices
Program to convert a given number to words
Count ways to reach the n'th stair
Fizz Buzz Implementation
Program to print prime numbers from 1 to N.
Singular Value Decomposition (SVD)
Find first and last digits of a number
Check if a number is Palindrome