+ 4

What does f*=i; really do in this code

#include <iostream> int main() { int f=1, i=2; while(++i<5) { f*=i; // what is this suposed to do ? } std::cout<<f<<std::endl; return 0; }

25th Apr 2017, 6:14 PM
Alexandre Lessard
6 Answers
+ 15
f *= i; is similar to f = f * i; It assigns the product of f and i to f for each loop.
25th Apr 2017, 6:15 PM
Hatsy Rei
Hatsy Rei - avatar
+ 10
Just leave it for future reference. :>
25th Apr 2017, 6:21 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Thank you i wasnt sure if that was the case for multiplications also. Should i delete a question after im answered or leave it there ?
25th Apr 2017, 6:19 PM
Alexandre Lessard
+ 3
it simply multiplies the variables f & i and stores the result in variable f. f*=i means f=f*i
25th Apr 2017, 7:02 PM
Paramasivam
Paramasivam - avatar
0
This was helpful. also, I used http://cpp.sh/ to run the following. Interesting exponential jumps with this loop/function/expression: / Example program using namespace std; #include <fstream> #include <iostream> #include <string> int main() { int f=1, i=2; while(++i<12) { f*=i; cout<<f << endl; } } Output: 3 12 60 360 2520 20160 181440 1814400 19958400
20th Aug 2021, 3:57 PM
Zachary Rex Theile
Zachary Rex Theile - avatar