How does the modulus operator work?

Let’s say that I need to format the output of an array to display a fixed number of elements per line. How do I go about doing that using modulus operation?

Using C++, the code below works for displaying 6 elements per line but I have no idea how and why it works?

for ( count = 0 ; count < size ; count++)
{
    cout << somearray[count];
    if( count % 6 == 5) cout << endl;
}

What if I want to display 5 elements per line? How do i find the exact expression needed?

Leave a Comment