How to do factorials | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to do factorials

HoW DO I DO FACTORIALS IN C++? I LOOKED IT UP AND IT SAID TO PUT AN ! AFTER THE VARIABLE BUT THAT GAVE ME AN ERROR. PLEASE HELP ME IM NEW TO c++.

9th Oct 2019, 1:59 AM
OnesAndZeroes10
OnesAndZeroes10 - avatar
5 Answers
+ 4
Chill with the caps lol. x! is mathematical notation for the factorial of x, in C++ you have to make a function for it. If you know how factorial is calculated you could attempt it on your own and if you run into any problems you can always search it up, look up examples, or ask here!
9th Oct 2019, 2:21 AM
jtrh
jtrh - avatar
+ 2
Sorry my iPad keyboard is stuck on CAPS for some reason when using SoloLearn. My phone is fine
9th Oct 2019, 2:22 AM
OnesAndZeroes10
OnesAndZeroes10 - avatar
+ 2
You saw something like this?: x!=y? If it is what you were talking about: It does not actually take factorial of x, it performs a comparison on x and y. x != y means that x is not equal to y. If they were equal, the comparison will evaluate to 0 (false), if they were not equal, the comparison will evaluate to 1 (true). (continued) You can easily calculate factorials using for loops. Example x = n!: int x = 1; for (int i = 1; i <= n; i++) { x *= i }
9th Oct 2019, 4:59 AM
Seb TheS
Seb TheS - avatar
0
thx
9th Oct 2019, 1:47 PM
OnesAndZeroes10
OnesAndZeroes10 - avatar
0
There is an elegant solution: int fac(int n) { return n<2?1:n*fac(n-1); } ...something like this.
9th Oct 2019, 3:36 PM
Petros Simidyan
Petros Simidyan - avatar