0

ex: first bus will transport 50 passengers, leaving 126-50=76 in the station. The next one will leave 26 in the station

tried to pass input value through if loop, to counter the problem, kindly correct this loop cin>>n; g=n; if(g>50){ g=g-50;} cout<<g;

20th May 2021, 12:26 PM
RAESEN
RAESEN - avatar
4 Respuestas
+ 1
Thnx for the new concept, &the best possible way I found 2use if statement as a loop: cin>>n; g=n; check: if(g>50){ g=g-50; } else{cout<<"//"<<g<<"//" ;} goto check; BUT JUST SPECIFY ANSWER IS ENCLOSED IN BTW //, AS IT IS INFINITE LOOP ✌🏻
22nd May 2021, 6:41 AM
RAESEN
RAESEN - avatar
+ 1
What is that g>>n? "if" statement is not 'loop'. >> is binary shift operator. Btw you assigned 'n' to 'g', why? All you have to do is calculate how many empty seats will be in the last bus. Try using modulo (operator%)
20th May 2021, 12:35 PM
Michal Doruch
0
My bad,it's g>>50, its easy to do with % but I want to try it with other ways, I can't use a for loop like for(g>>50), to divides n values in pairs of 50 every single time!
20th May 2021, 12:42 PM
RAESEN
RAESEN - avatar
0
Then you are right-shifting g. It makes no sesne at all By doing 126>>50 you are doing something like dividing: 126/(2^50) This number is so small, it will probably get rounded to 0 (it actually will be just 0). 00000000000000000000000001111110 is binary representation of 126. You try to right-shift it by 50 which means you try to move all position 50 places to right. All these ones will "go out" turning into zeros. Check it for better understanding: https://www.geeksforgeeks.org/left-shift-right-shift-operators-c-cpp/
20th May 2021, 2:25 PM
Michal Doruch