19

I was searching around about this topic but I still don't get it, if someone can elaborate I would be very thankful.

My task is to divide two variables as integer division with remainder.. The problem is, that I have no clue what a remainder is, for now I did something like that, this is what I found by searching through the internet:

int a;
int b;
int remainder = Math.Pow(a,2) % b;

System.out.println("a^2 / b = " + Math.Pow(a,2) / b);
System.out.println("remainder = " + remainder);

if I for example set (a = 43) and (b = 67)

Then I will get this reslut:

a^2 / b = 27
remainder = 40

Now since I have no idea what the remainder is (this is just a suggestion form the internet) I have no idea if this is the correct answer..?

Thanks for any help,

Kind Regards

DisasterCoder
  • 477
  • 1
  • 3
  • 17
  • 7
    In an integer division the remainder is what doesn't fit into the divisor, i.e. if you calculate 11/4 the integer result will be 2 (4 fits 2 times into 11) and a remainder of 3 (since 11 - 2*4 = 3). Or put another way: if you have 11 apples and want to give them equally to 4 people you can only give each person 2 apples and then have 3 left (remaining). – Thomas Sep 25 '15 at 09:11
  • "since I have no idea what the remainder is" - so you not understand what remainders are? 43*43 is 1849, and 1849 divided by 67 is 27, with a remainder of 40 - as 27 * 67 = 1809. It's not clear what your question is here. It's a little like saying "here's code to do addition... it says that 4 + 5 is 9, but I'm not sure whether that's right or not". – Jon Skeet Sep 25 '15 at 09:13
  • Okay thanks.. english is not my native tongue so maybe that's why I didn't understand.. and also because I suck at maths. – DisasterCoder Sep 25 '15 at 09:21

6 Answers6

27

If you are looking for the mathematical modulo operation you could use

int x = -22;
int y = 24;
System.out.println(Math.floorMod(x, y));

If you are not interested in the mathematical modulo (just the remainder) then you could use

int x = -22;
int y = 24;
System.out.println(x%y);
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • 2
    If you need the modulo calculation (for a positive `y`) you can also perform the trick `((x % y) + y) % y`, but beware that this may cause an integer overflow (for very large values of `y`). It does however skip the function call. This is an optimization however, and should be applied only where required. – Maarten Bodewes Feb 13 '19 at 16:17
8
    public static void main(String[] args) {
        int dividend = 139, divisor = 7;

        int quotient = dividend / divisor;
        int remainder = dividend % divisor;

        System.out.println("The Quotient is = " + quotient);
        System.out.println("The Remainder is = " + remainder);
    }

Output:

The Quotient is = 19

The Remainder is = 6

Taras Melnyk
  • 3,057
  • 3
  • 38
  • 34
2

Yes, the % operator will return the remainder of the Integer division.

To know more about the remainder of the Integer division check out Wikipedia:

If a and d are integers, with d non-zero, it can be proven that there exist unique integers q and r, such that a = qd + r and 0 ≤ r < |d|. The number q is called the quotient, while r is called the remainder.

João Neves
  • 944
  • 1
  • 13
  • 18
1

int remainder = a % b; will sort you. The remainder operator returns the remainder of the division.


Note that the remainder operator is also called the modulo operator. However, this is incorrect for Java as Java will return a negative value if the left operand a is negative.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
hd1
  • 33,938
  • 5
  • 80
  • 91
  • 4
    It's not a modulo operator. It's a remainder operator. They aren't the same thing. – Jon Skeet Sep 25 '15 at 09:14
  • @DisasterCoder depends on your specifications of implementation. Do you want it to be a simple remainder(it can have negative values), or the mathematical correct modulo operation. – SomeJavaGuy Sep 25 '15 at 09:21
  • For this task I believe it's only the simple remainder that is asked, however to know both ways would be best right? So what's the mathematical correct modulo operation? – DisasterCoder Sep 25 '15 at 09:25
0

% operator will return the remainder of the Integer division.

What modules actually does under the hood ?

Modules tend to remove cycles from the number, until it reaches a positive number that is smaller than the number of the cycle which we call modulo OR a negative number which we call a reminder.

However, Using % operator is time expensive.

To avoid using % while getting the same result, we can use the following:

  • While(a >= n) a -= n; (when a is a positive number)
  • While(a < 0) a += n; (when a is a negative number)

  • a = n*q + r that means r = a - n*q While q is the integer division of a/n which means a%n == a - n * Math.toIntExact(a/n) Which is sufficient when a is a positive number.

  • While a is a negative number, we can use (a%n + n) % n Which will give you module.

Case Scenario on Clock:

if it is now 9 O'clock, what time after 4 hours => 9+4 = 13h => 13%12=1 while 12 is the cycle number in the clock

What if we need to calculate time before 24 hours (Yesterday) from now which is 9 O'clock, then: 24(2*12) => Yesterday Means 9-24 = -15h While the right answer is 9 , to solve this we will use (a%n + n) % n While a%n == (a - n * Math.toIntExact(a/n)) then -15 - 12 * Math.toIntExact(-15/12) = -3 => -3 + 12 = 9 => 9%12 => 9 - 12 * Math.toIntExact(9/12) = 9 Which is the right answer.

This is the code for the clock Scenario:

public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);
    int a = scanner.nextInt(); // a = -15
    int n = scanner.nextInt(); // cycle = 12

    int reminder = a - (n * Math.toIntExact(a / n));
    int reminder_plus_n = (reminder + n);
    int modulo = reminder_plus_n - (n * Math.toIntExact(reminder_plus_n / n));
    System.out.println(modulo); // Answer = 9
} 
Abdel-Raouf
  • 700
  • 1
  • 8
  • 20
0

You can use the quotient together with the remainder to calculate the count and remainder.

Here is some code that I wrote for a Minecraft plugin.

public class Division {
    public static ModuloDivResult divideMod(double a, double b) {
        double remainder = a % b;
        return new ModuloDivResult((a / b) - (remainder / b), remainder);
    }
}

We also need a container for both values.

public class ModuloDivResult {
    private double remainder;
    private double count;

    public ModuloDivResult(double count, double remainder) {
        this.remainder = remainder;
        this.count = count;
    }

    public double getRemainder() {
        return remainder;
    }

    public double getCount() {
        return count;
    }
}
Orbyfied
  • 94
  • 11