int iAdmited = 50;
System.Collections.ArrayList objArrayList = new System.Collections.ArrayList();
objArrayList.Add(12);
objArrayList.Add(15);
objArrayList.Add(20);
objArrayList.Add(25);
objArrayList.Add(30);
objArrayList.Add(35);
objArrayList.Add(40);
objArrayList.Add(45);
objArrayList.Add(55);
objArrayList.Add(60);
System.Collections.ArrayList objValue = new System.Collections.ArrayList();
// loop through the given values
foreach (int objInt in objArrayList)
{
if (objInt <= iAdmited)
{
// add the values under 50 to our arraylist so that we can use it later
objValue.Add(objInt);
}
}
int iValueEnd = 0;
// loop throught the collection of values under 50
foreach (int iValue in objValue)
{
if (iValueEnd < iAdmited)
{
// add the value
iValueEnd = iValueEnd + iValue;
// if the value added makes the end result heigher then 50 it will be removed
if (iValueEnd >= iAdmited)
{
iValueEnd = iValueEnd - iValue;
}
}
}
/// End Result will be 47
MessageBox.Show(iValueEnd.ToString());