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++.
5 Réponses
+ 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!
+ 2
Sorry my iPad keyboard is stuck on CAPS for some reason when using SoloLearn. My phone is fine
+ 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
}
0
thx
0
There is an elegant solution:
int fac(int n)
{
return n<2?1:n*fac(n-1);
}
...something like this.