0

what are the uses of % in c++

eg: { inches = (d1.inches + d2.inches) % 12; feet = (d1.feet+d2.feet) + ((d1.inches+d2.inches) / 12); }

12th Oct 2016, 2:46 PM
Luqman Ahmed
Luqman Ahmed - avatar
3 Respuestas
+ 3
% (modulus) gives you the remainder of the euclidian division. For example: 42 % 5 is 2 because 42 = 5*8 + 2 x%y is 0 if x is a multiple of y x%2 is 0 if x is even and 1 if x is odd x%10 gives you the last digit of x x%100 gives you the last two digits of x etc.
12th Oct 2016, 2:54 PM
Zen
Zen - avatar
+ 2
It's the remainder of a division. for example: 16:3= 5 remainder 1 so 16:3=5 16%3=1 it's useful, for example, with time: 170 seconds are 2 minutes and 50 seconds infact 170:60=2 minutes 170%60=50 seconds in your case, probably it's a matter of imperial units: feet=inches/12 inches=inches%12
12th Oct 2016, 2:54 PM
marcram
+ 1
% is modulus or remainder operator if you write in code 40%9 it gives output 4 which is remaider. in above code it add values of inches of d1 and d2 and after dividing it with 12 gives the value of remainder as output
12th Oct 2016, 2:58 PM
ZUNAIRA
ZUNAIRA - avatar