+ 1

Modulo example

Im looking for a situation wehere you need the modulo operator

18th Jan 2017, 3:18 PM
Eyup Sogukcesme
Eyup Sogukcesme - avatar
6 Answers
+ 8
So i used it in a lot of places already. Basic example is to shift an array. So i'll illustrate it: You have an array with 3 values and you want to shit it 1 place to the left and save as second array So you creating basic loop and fo each element you are getting shifted one: arr[i] = arr[(i + 1) % len(arr)] Now for 0th element we are getting element # (0 + 1) % 3 # 3 is array length(example) so index will be 1 % 3 = 1 But for last element: arr[2] = arr[(2 + 1) % 3] Index will be 3 % 3 = 0, it will close the cycle
18th Jan 2017, 3:32 PM
WittyBit
WittyBit - avatar
+ 4
Think division, but you are looking for the remainder fraction. You want the result to be exactly equal giving you 0, and not have excess - after the period. int a = 13, b = 2; if (a % b == 0) { cout << "Great"; }else{ cout << "Not great"; } This will output "Not great" because 13 % 2 = 5. If a was 12, the output would be "Great" as they are shared equally with no remainder fraction || piece.
19th Jan 2017, 1:31 AM
Mark Foxx
Mark Foxx - avatar
+ 2
May b odd even example. If a number divided by 2 is equal to 0 then number is even else odd.
18th Jan 2017, 3:25 PM
Divesh Agarwal
Divesh Agarwal - avatar
+ 1
Best examples are to find a number whether it is even or odd, whether a number is prime or not.
15th Feb 2017, 1:43 PM
Kommoju Sunil Kumar
Kommoju Sunil Kumar - avatar
0
As many people are used to packages and modules while writing a code we are not coming across using modulo operations as such... One obvious thing I can say is it is used in date module for sure...
18th Jan 2017, 5:06 PM
Sai Kiran Rayapureddy
Sai Kiran Rayapureddy - avatar
0
What about a clock
8th Feb 2017, 9:13 PM
Marios Magioladitis
Marios Magioladitis - avatar