Hey guys,
I'm making a program that takes an input and chucks out the factorial result.
Now the program just chucks out what i put into it when i input 5 when I'm pretty sure it should output 120.
If anyone can point out where im going wrong it'd be appreciated.
code below:
i changed the resultNumber =number to resultNumber =1 then changed resultNumber = resultNumber * 1 to resultNumber = resultNumber * number as that made more sense but now its just chucking out 0
sorry lowest one didnt see your reply but i already declared number before that and as far as i know i still have to put the ; there even if nothings going in it.
the number going in should be 5 then it gets reduced by 1 per iteration until it becomes less then 1 or equal to it which then stops the for loop
Or thats whats supposed to happen in my mind i could be wrong.
ahh took a while but it finally hit me i swapped the < to a > and changed the cout line to output resultNumber instead of temp.
It now works properly thanks alot for the help.
silly mistakes make me feel stupid but least i learn from them.
hey just one last thing if i input 20 then it outputs a -number which im guessing is because of the size limit of an int so i tried putting long int for resultInteger instead but its still doing it.
Use unsigned long long for maximum storage capacity, any larger than that and you'll have to look into either making your own way to handle larger numbers or finding libraries to do it for you. Long can map to the same type as int, which it sounds like it did in your case.
thank you that solved that little problem :P ive done factorial through recursion now aswell which didnt take aslong as i thought it would. having problem with it calling my functions through ive tried both switch and if statements. when i try to launch a function with the if statements it works fine but if i try to make it call them through an if or switch it just skips them completely.
oh damn forgot the ' '.
it has to hit one so the user can make a selection between either iterative or recursive and using if statements to allow for the option seemed like the easiest way as switch seemed excessive for only two options.
also cosmin i need a class as its part of the work that we need to make a separate class that has an iterative function and recursive which work out the factorial.