Pages

Friday, August 3, 2012

Get the quotient using addition and subtraction only.

private string getQuotient(int dividend, int divisor)
{
  string result = "";
  int wholeNumber = 0;
            
  // loop until the dividend will result a positive integer
  while ((dividend > 0) && (dividend >= divisor))
  {
    dividend -= divisor;
    wholeNumber++;
  }

  result = wholeNumber.ToString() + " r. " + dividend.ToString();
            
  return result;
}