Learn AP Comp Sci

Problem of the Day

Friday, November 8, 2024


Problem:

Consider the following method.

public static helper(int m, int n)
{
int c = 0;
while (m > n)
{
m -= n;
c += 1;
}
}

Which of the following operations does the method helper implement?

  1. calculating a power, mn
  2. integer division, m / n
  3. integer division, m % n
  4. integer multiplication, m * n

Show solution:

The correct answer is b. The variable c counts how many times n can be subtracted from m, or (stated another way) how many times n can evenly "go into" m. This is integer division.