• Status: Solved
• Priority: Medium
• Security: Public
• Views: 427

# C# Question on Modulus

I have a requirment for my new customer in our new e commerce website..I know I have to use modulus for solving this problem but need some help.

I have a requirement like

1 kit will have 100 pieces.
if Number of employees is 500 i have to allocate 5kits. if employees are 503 then I have to allocate 6 kits.

thanks in advance for helping me
0
SubbuUSA
• 2
• 2
2 Solutions

Commented:
Modulus is the remainder.

500 / 100 = 5

500 % 100 = 0

503 / 100 = 5

503 % 100 = 3

``````int num_of_emp = 503;

int num_of_kits = num_of_emp / 100;

if(num_of_emp % 100 != 0)
{
num_of_kits++;
}
``````
0

Author Commented:
thanks.  what if I have a configurable value instead of 100. say 5 or 10. what if the value 503 is less than the configurable value.

0

Commented:
Alternatively:

``````int piecesPerKit = 100;
int employees = 503;
int kits = (int)Math.Ceiling((double)employees / piecesPerKit);
``````
0

Commented:
wdosanjos response will also work but assuming you want to use the modulus operator.

The value will not matter.

``````int num_of_emp = 73;
int piecesPerKit = 100;

int num_of_kits = num_of_emp / piecesPerKit ;  // will yeild 0

if(num_of_emp % piecesPerKit != 0) // modulus will yield 73
{
num_of_kits++;  // you will get 1 kit
}
``````
0

Author Commented:
Thanks to you both
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.