Share your repls and programming experiences

← Back to all posts
Factorials!
Bunnytoes (28)

Took a while. Don't do over 31 and if you do it says error. Credit to @malvoliothegood for inproving the code.

Comments
hotnewtop
malvoliothegood (852)

I am wondering if you are getting the 'right' answer when it is quite large. Remember that the int datatype can store a maximum positive value of 2147483647 (ref: https://www.w3schools.com/cs/cs_data_types.asp). What do you think happens when this limit is reached? Hint try the number 17 as input.

You could use the long datatype, but even this maxes out quite quickly. See this fork of your program: https://replit.com/@malvoliothegood/Factorial-test-1#main.cs

Bunnytoes (28)

@malvoliothegood thanks for making it better. This was just a test for me to see it I can do it. And I will credit you but mind if I use your code?

malvoliothegood (852)

@Bunnytoes Yes, as long it is not for some assessment, and you understand the code. Otherwise it is best to use your own code.

There is a bug in the program though. If n is big enough fact will have wrapped around enough to be in the positive range, then the if (fact < 0) test won't do its job. There is a way around this though - you could test values of n until a negative value for fact occurs, and then enforce this limit on the user.

Bunnytoes (28)

@malvoliothegood hmmn, that is a good suggestion, but I don't know how to impletment it. Thanks again though!