Recently, I had a simple coding task. I needed to split an item's quantity across multiple lines.
For example, I had Item 1 with a total quantity of 10, but that quantity could not remain on a single line. It had to be distributed across several lines, based on related sub-items.
At first, this sounded like a very small task. Still, it turned into a good reminder that many real problems are solved not by advanced patterns, but by understanding the basics well.
In this case, the solution came from remembering the modulo operator (%).
What % does
The modulo operator returns the remainder after division.
10 % 3 // 1
This means that if you divide 10 into 3 equal parts, each part gets 3, and 1 is left over.
That leftover value is exactly what makes % so useful when distributing quantities.
My case, splitting quantity per line
Let’s say we have:
- Total quantity:
10
- Number of lines:
3
int quantity = 10;
int lines = 3;
int baseQty = quantity / lines; // 3
int remainder = quantity % lines; // 1
Now we know:
- each line gets at least
3
- there is
1 extra item left
- that extra item can be added to the first line
for (int i = 0; i < lines; i++)
{
int lineQuantity = baseQty + (i < remainder ? 1 : 0);
Console.WriteLine($"Line {i + 1}: {lineQuantity}");
}
Output:
Line 1: 4
Line 2: 3
Line 3: 3
So with a very simple use of %, we can split quantities as evenly as possible.
More useful examples of %
1. Check if a number is even or odd
int number = 7;
if (number % 2 == 0)
{
Console.WriteLine("Even");
}
else
{
Console.WriteLine("Odd");
}
This works because even numbers divide by 2 with no remainder.
2. Find numbers divisible by 5
for (int i = 1; i <= 20; i++)
{
if (i % 5 == 0)
{
Console.WriteLine(i);
}
}
This is useful when you want to trigger something every 5th item, every 10th row, and so on.
3. Alternate row styles in a grid
for (int row = 0; row < 6; row++)
{
string style = row % 2 == 0 ? "Light" : "Dark";
Console.WriteLine($"Row {row}: {style}");
}
This is a very common use in UI, where rows alternate between two styles.
4. Cycle through a list of values
string[] colors = { "Red", "Green", "Blue" };
for (int i = 0; i < 10; i++)
{
Console.WriteLine(colors[i % colors.Length]);
}
This lets you repeat values in a loop without going out of bounds.
5. Print a new line every 4 items
for (int i = 1; i <= 12; i++)
{
Console.Write(i + " ");
if (i % 4 == 0)
{
Console.WriteLine();
}
}
Useful for formatting output into groups.
6. Round-robin assignment
string[] users = { "Anna", "John", "Maria" };
for (int task = 0; task < 8; task++)
{
string assignedTo = users[task % users.Length];
Console.WriteLine($"Task {task + 1} -> {assignedTo}");
}
This is a clean way to distribute work in rotation.
Why this matters
What I like about examples like this is that they bring us back to an important truth. We often focus on learning bigger frameworks, more advanced architectures, and more complex tools. But again and again, the real solution comes from something basic.
Operators like %, loops, conditions, and simple arithmetic are not small details. They are part of the foundation, and they solve real business problems every day.
That is why reviewing the basics is always worth it. Sometimes the cleanest solution is not hidden in something advanced, but in something we already learned a long time ago.