How do you get the factorial of a number with just addition and subtraction ? lets say i have to find 5!
just define multiplication in terms of addition (and a loop)
and you're probably meant to learn and use the principles of recursion (in which case you don't even need the loop).
What language are you using? I do Python, so I can explain it using that. Have two variables x and y. y is x - 1 Multiply x and y together, and that is your new variable for x. Subtract one from y, and that is now y. Do all this while the variable for y is greater than 0, or greater than or equal to 1.
you multiplied
thank u all i fix it
so how much faster is this (meant to be) than just letting the compiler do the work on int fact(int a) { if (a<3) return a; return a*fact(a-1); } ? Also, could you close the question at some point?
Join our real-time social learning platform and learn together with your friends!