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

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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` ` `  `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 "````, ` `                                 ``findMaxValue()); ` `    ``return` `0; ` `} `

## 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()); ` `    ``} ` `} `

## 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 `

## PHP

 ` 0) ` `            ``break``; ` `        ``\$res``++; ` `        ``\$fact` `= ``\$fact` `* ``\$res``; ` `    ``} ` `    ``return` `\$res` `- 1; ` `} ` ` `  `// Driver Code ` `echo` `"Maximum value of"``.  ` `           ``" integer "` `.  ` `         ``findMaxValue(); ` `     `  `// This code is contributed by Sam007 ` `?> `

/div>

Output :

```Maximum value of integer : 20
```

## tags:

Mathematical factorial Mathematical factorial