0
Functions
Write a program to calculates the factorial of a number when entered. The program should contain a function which purposely does the calculation.
5 Antworten
+ 6
Writing a factorial function is usually used to introduce recursion:
int fact(int n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
Implemented with a loop:
int fact(int n) {
int result = 1;
for (; n > 0; --n)
result *= n;
return result;
}
+ 5
yea i could give u the answer but then again part of being a programmer is problem solving. so u needa work on that
start by understanding what a factorial is
2! = 2 * 1
3! = 3 * 2 * 1
6! = 6 * 5 * 4 * 3 * 2 * 1
the basic cases u should know about are
1! = 1
0! = 1
+ 3
or u could be the squidy guy that doesnt let you figure it out and practice by yourself
+ 3
Tnx guys
+ 2
i will give hint
try loop and each time multiply by the number-1