+ 3

How modouls operator work in while loop?

In C++ Help to Explain these codes https://code.sololearn.com/cbA49Y73EAuG/?ref=app https://code.sololearn.com/c1L3w0xh1p45/?ref=app

27th Dec 2018, 7:05 AM
Saad Saleem
Saad Saleem - avatar
13 Answers
+ 8
* 1st code. int x = 7; while(x%3)//repeat while x % 3 is non zero { x += (x-2);// x is now 12 (7 + (5)) x /= 2;//x is now 6 (12 / 2) } // loop stops because 6 % 3 == 0 cout << x;// you get 6 * 2nd code. int x = 25; while(x%3)//repeat while x % 3 is non zero { x++;// x is incremented ... // x = 26, loop repeats (26 % 3 = 2) // x = 27, loop will stop (27 % 3 = 0) } cout << x;// you get 27 I think it goes like this : )
27th Dec 2018, 7:41 AM
Ipang
+ 6
You have to declare a fixed condition like this code :- (Corrected code of Quiz while) https://code.sololearn.com/cy2oKYyPWXE3/?ref=app https://code.sololearn.com/cMx23Dhl4ATQ/?ref=app
27th Dec 2018, 7:08 AM
Bipin Tatkare
Bipin Tatkare - avatar
+ 4
You not have the proper condition of (x % 3) so every value of x must be satisfied in while condition
27th Dec 2018, 7:43 AM
Bipin Tatkare
Bipin Tatkare - avatar
+ 4
They are not being multiplied! The sole responsibility of multi(x-1, y) is pushing the number 5 on the stack for 5 times as x = 5, push 5 x = 4, push 5 x = 3, push 5 x = 2, push 5 x = 1, push 5 x = 0, beginning of the recursive addition as above. On the paper you have 5 + 5 + 5 + 5 + 5 = 25
27th Dec 2018, 10:04 AM
Babak
Babak - avatar
+ 3
Ipang Thanks
27th Dec 2018, 7:45 AM
Saad Saleem
Saad Saleem - avatar
+ 3
#include <iostream> using namespace std; int multi(int x, int y) { int sum = 0; if (x == 0) return 0; else return multi(x - 1, y) + y; } int main() { cout << multi(5, 5); return 0; } /* Stack status when x == 0 | 5 | | 5 | | 5 | | 5 | | 5 | That is, the program has stored the value of `y` 5 times in the stack frame Popping the stack's content from above yields return 0 + 5 = 5 return 5 + 5 = 10 return 10 + 5 = 15 return 15 + 5 = 20 return 20 + 5 = 25 And as can be seen, the last return would return the correct result of the multiplication. */
27th Dec 2018, 9:42 AM
Babak
Babak - avatar
+ 2
The while condition returns remainder for division If it is zero the while loop takes it as Boolean false If it is nonzero it is taken as Boolean true
27th Dec 2018, 7:12 AM
Dheeraj Tiwari
Dheeraj Tiwari - avatar
+ 2
(x%3) is treated by the compiler to be the same as (x%3!=0)
27th Dec 2018, 7:25 AM
Dheeraj Tiwari
Dheeraj Tiwari - avatar
27th Dec 2018, 7:45 AM
Saad Saleem
Saad Saleem - avatar
+ 1
I understand how While(x%3 != 0) & while(x%3 == 1) Works But how while(x%3) Works
27th Dec 2018, 7:22 AM
Saad Saleem
Saad Saleem - avatar
+ 1
dheeraj Bipin Tatkare - (B. R. T) Thanks guys i understand how these codes work
27th Dec 2018, 7:34 AM
Saad Saleem
Saad Saleem - avatar
+ 1
OK I get it
27th Dec 2018, 10:07 AM
Saad Saleem
Saad Saleem - avatar
0
But why the value inside the function multi(x-1,y) multiplying
27th Dec 2018, 9:51 AM
Saad Saleem
Saad Saleem - avatar