The Problem:
A box can hold 53 items. If a person has 56 items, it will require 2 boxes to hold them. Box 1 will hold 53 items and box 2 will hold 3.
How do I repeat the above where 53 is a constant, unchanging, value and 56 is a variable for each box:
Math.Ceiling(Convert.ToDecimal(intFeet / 53))
what I have so far for that is:
int TotalItems = 56;
int Boxes = Math.Ceiling(Convert.ToDecimal(intFeet / 53));
for (int i = 0; i < Boxes; i++)
{
int itemsincurrentbox=??
}
The Python count() function works out how many times a value appears in a list or a string. It returns the number of times the value appears as an integer.
out. println(firstNumber + " goes into " + secondNumber + " " + total + " times. The remainder is " + remainder + "."); In java int\int=int , i.e. your total field already gives you the number of whole times.
An integer n can be divided by 2: floor(log(n)/log(2)) times.
Where the integers capacity
and numItems
are your box capacity (53 in the example) and the total number of items you have, use the following two calculations:
int numBoxes = numItems / capacity;
int remainder = numItems % capacity;
This will give you the number of boxes that are filled (numBoxes
), and the number of items in an additional box (remainder
) if any are needed, since this value could be 0.
Edit: As Luke pointed out in the comments, you can get the same result with the .NET class library function Math.DivRem.
int remainder;
int numBoxes = Math.DivRem( numItems, capacity, out remainder );
This function returns the quotient and puts the remainder in an output parameter.
simple, overly imperative example:
int itemsPerBox = 53;
int totalItems = 56;
int remainder = 0;
int boxes = Math.DivRem(totalItems, itemsPerBox, out remainder);
for(int i = 0; i <= boxes; i++){
int itemsincurrentbox = i == boxes ? remainder : itemsPerBox;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With