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.

5th Apr 2017, 9:57 PM
grace barnoh
grace barnoh - avatar
5 Respostas
+ 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; }
5th Apr 2017, 10:19 PM
Squidy
Squidy - avatar
+ 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
5th Apr 2017, 10:17 PM
Edward
+ 3
or u could be the squidy guy that doesnt let you figure it out and practice by yourself
5th Apr 2017, 10:20 PM
Edward
+ 3
Tnx guys
5th Apr 2017, 10:27 PM
grace barnoh
grace barnoh - avatar
+ 2
i will give hint try loop and each time multiply by the number-1
5th Apr 2017, 10:06 PM
Moatazbellah Mahmoud
Moatazbellah Mahmoud - avatar