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

`// 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 Code ` `int` `main() ` `{ ` ` ` `printf` `(` `"Maximum value of integer : %d\n"` `, ` ` ` `findMaxValue()); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum value of ` `// an integer for which factorial can be ` `// calculated on your system ` `import` `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()); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find maximum value of ` `# an integer for which factorial can be ` `# calculated on your system ` `import` `sys ` `def` `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 Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `print` `(` `"Maximum value of integer:"` `, ` ` ` `findMaxValue()); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum value of ` `// an integer for which factorial can ` `// be calculated on your system ` `using` `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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find maximum ` `// value of an integer for which ` `// factorial can be calculated ` `// on your system ` `function` `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 Code ` `echo` `"Maximum value of"` `. ` ` ` `" integer "` `. ` ` ` `findMaxValue(); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

**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 contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the last digit when factorial of A divides factorial of B
- Elements of Array which can be expressed as power of some integer to given exponent K
- Biggest integer which has maximum digit sum in range from 1 to n
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find maximum power of a number that divides a factorial
- Maximum number with same digit factorial product
- Minimum decrements to make integer A divisible by integer B
- Maximum number of region in which N non-parallel lines can divide a plane
- Find the maximum angle at which we can tilt the bottle without spilling any water
- Find the maximum possible value of the minimum value of modified array
- Smallest integer which has n factors or more
- Find an integer X which is divisor of all except exactly one element in an array
- Smallest integer > 1 which divides every element of the given array
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if an integer can be expressed as a sum of two semi-primes
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Count digits in a factorial | Set 2
- Count trailing zeroes in factorial of a number