+ 1

How to find factorial

#include <iostream> using namespace std; int main() { int num=1; for(int i=6;i>=1;i++){ i=i*num; cout<<i; } //cout<<i; return 0; }

29th Nov 2023, 8:24 AM
Haram Abbas Lar
Haram Abbas Lar - avatar
6 Respostas
+ 1
Try the for loop with i—
29th Nov 2023, 8:29 AM
JaScript
JaScript - avatar
+ 1
Can you please explain it
29th Nov 2023, 8:29 AM
Haram Abbas Lar
Haram Abbas Lar - avatar
+ 1
For the beginning of for loop you set the i to 6. The end of counter you set to 1. Thus you have to count down, what you do with i—. Ech time you multipicate num with i and so get factorial. 6! = 6*5*4*3*2*1
29th Nov 2023, 8:39 AM
JaScript
JaScript - avatar
+ 1
Remember your loop syntax: For(start value; condition; increment) In your example, you're starting with 6, and have set your condition to activate while i is greater than or equal to 1. You then asked it to add 1 to i each cycle. This means that the value of i will go 6,7,8,9 etc and never satisfy the condition. If you start at 6 and need to get to 1, i should be counting down. Amend your loop to: For(int i = 6; i >= 1; i--) You also then need to switch your num and I variables in the loop, as you are just repeatedly updating i each loop. try this: #include <iostream> using namespace std; int main() { int num=1; int res{}; for(int i=6;i>=1;i--){ num *= i; cout<<i << endl;; } cout<<num; return 0; }
29th Nov 2023, 9:35 AM
StuartH
StuartH - avatar
+ 1
just first try it by yourself , tips -> it need a recursive function
30th Nov 2023, 4:20 PM
Alhaaz
Alhaaz - avatar
0
Hi
30th Nov 2023, 5:09 PM
advait chaudhary
advait chaudhary - avatar