Practical use of % in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Practical use of % in C++

So, now I know what means % in C++, I started wanering what is practical use for % in C++, Can you please explain me why you would need a remainder from division for a good program?

29th Nov 2016, 4:01 PM
MysticMM
MysticMM - avatar
6 Answers
+ 9
i will try to describe circular index if you want to iterate over a fixed sized array of size let's say 5. so one way is to have a variable for running index which will keep on increasing 0,1,....,until program is finished but you only want to go over the array which have 5 slots so you want your index variable to start over once it reached the size of the array (5 for this example) so if you take 'i' as the index variable, and apply modulo of the size of the array, you will end up in taking the remainder each time, as follows i i%5 0 0 1 1 2 2 3 3 4 4 5 0 6 1 7 2 . . . and so on
29th Nov 2016, 7:01 PM
Burey
Burey - avatar
+ 6
circular index for a list check if a number is divisable with another number those just at the top of my head there many many more
29th Nov 2016, 4:08 PM
Burey
Burey - avatar
+ 4
finding gcd/hcf, lcm etc. Other maths based problems and algorithms. Building different patterns using loops. I think there are endless uses you can't sum up in one question.
29th Nov 2016, 4:24 PM
Mohammed Maaz
Mohammed Maaz - avatar
0
to check even or odd numbers by remainder division with 2
29th Nov 2016, 4:10 PM
Mohammed Maaz
Mohammed Maaz - avatar
0
And what is circular index?
29th Nov 2016, 4:10 PM
MysticMM
MysticMM - avatar
0
Oh yea I did that, is there any more uses?
29th Nov 2016, 4:21 PM
MysticMM
MysticMM - avatar