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

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
Asked:
SubbuUSA
  • 2
  • 2
2 Solutions
 
mac-willCommented:
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++;
}

Open in new window

0
 
SubbuUSAAuthor 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.

Thanks for your response mac-will:
0
 
wdosanjosCommented:
Alternatively:

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

Open in new window

0
 
mac-willCommented:
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
}

Open in new window

0
 
SubbuUSAAuthor 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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now