+ 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?
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
+ 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
+ 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.
0
to check even or odd numbers by remainder division with 2
0
And what is circular index?
0
Oh yea I did that, is there any more uses?