0

Is there any function in .Net to find out how many times a number is contained in another number with 0 remainders for example:

  • 10 and 2 = 5 times
  • 7 and 2 = 3 times
  • 3 and 2 = 1 time
K.D. Code
  • 79
  • 1
  • 5
Marc
  • 2,023
  • 4
  • 16
  • 30

3 Answers3

3

There are several options doing so:

Community
  • 1
  • 1
Graphican0
  • 167
  • 11
2

Integer division is probably what you need. it always result as same as Floor in math.

How ever there are some methods in Math class that may be useful. like DivRem. there is also overload that accepts long type.

int remainder;
int quotient = Math.DivRem(7, 2, out remainder); 
int whatYouWant = quotient;
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
1

You mean Remainders. Solution:

int a = 9;
int b = 4;
int result = (a - a % b) / b;

The % means modulus so this formula first removes what would be the remainder and then performs the division. Similarly you can find the remainder without doing the division using:

int result = a % b;
Steve Harris
  • 5,014
  • 1
  • 10
  • 25