I made a program to calculate factorial of the given number. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

I made a program to calculate factorial of the given number.

But when I enter 20 or greater, it shows negative value . Please tell me where I am missing. This is the code... (The main code) int factorial = 1; Console.Write("Enter the no. : "); int num = Convert.ToInt32(Console.ReadLine()); for(int x=1;x<=num;x++){ factorial *= x; } Console.WriteLine(

quot;The factorial of {num} is {factorial}"); Please help.

14th Jun 2020, 11:10 AM
Manas Tripathi
Manas Tripathi - avatar
2 Réponses
+ 2
integer is not big enough, so this overflow. Make it a double or a long or even bigger data type. https://stackoverflow.com/questions/19230573/cannot-calculate-factorials-bigger-than-20-how-to-do-so
14th Jun 2020, 11:49 AM
sneeze
sneeze - avatar
0
Thanks buddy
14th Jun 2020, 12:18 PM
Manas Tripathi
Manas Tripathi - avatar