Java Long remainderUnsigned Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the remainderUnsigned() method of the Long class in Java programming language. This method is used to return the remainder (unsigned) obtained by dividing the first argument with the second argument. The result i.e the remainder is always taken as an unsigned value.

Create a Java file

First, create a java file named RemainderUnsignedMethod.java in the ~/project directory using the following command:

touch RemainderUnsignedMethod.java

Then open the file using a text editor.

Write the code to find the remainder Unsigned

In this step, write the code to find the remainder as unsigned.

public class RemainderUnsignedMethod {

    public static void main(String[] args) {

        long dividend = 100L;
        long divisor1 = 5L;
        long divisor2 = -3L;

        // Finding unsigned remainder of positive and negative values
        long remainder1 = Long.remainderUnsigned(dividend, divisor1);
        long remainder2 = Long.remainderUnsigned(dividend, divisor2);

        System.out.println("Remainder of " + dividend + "/" + divisor1 + " is " + remainder1);
        System.out.println("Remainder of " + dividend + "/" + divisor2 + " is " + remainder2);

    }
}

Compile and run the code

In this step, compile and run the RemainderUnsignedMethod.java file using the following command:

javac RemainderUnsignedMethod.java && java RemainderUnsignedMethod

User Input

In this step, write a code to get the input of the user for dividend and divisor values.

import java.util.Scanner;

public class RemainderUnsignedMethod {

    public static void main(String[] args) {

        try {
            System.out.print("Enter the Dividend: ");
            Scanner sc = new Scanner(System.in);
            long dividend = sc.nextLong();

            System.out.print("Enter the Divisor: ");
            long divisor = sc.nextLong();

            /**
             * Finding unsigned remainder of Dividend and Divisor
             **/
            long remainder = Long.remainderUnsigned(dividend, divisor);
            System.out.println("Remainder of " + dividend + "/" + divisor + " is " + remainder);

        } catch (Exception e) {
            System.out.println("Invalid Input!!");
        }

    }

}

Compile and run the code

In this step, compile and run the RemainderUnsignedMethod.java file using the following command:

javac RemainderUnsignedMethod.java && java RemainderUnsignedMethod

Decimal Input Example

In this step, let's run the program with some inputs to test.

Enter the Dividend: 87
Enter the Divisor: 9
Remainder of 87/9 is 6

Negative Input Example

In this step, let's run the program with negative inputs to test.

Enter the Dividend: 333
Enter the Divisor: -11
Remainder of 333/-11 is 333

Invalid Input Example

In this step, let's run the program with invalid input to test.

Enter the Dividend and Divisor: 0x556 90
Invalid Input!!

Summary

In this lab, we have learnt the following:

  • remainderUnsigned() method of the Long class and its syntax;
  • How to get the remainder Unsigned in Java;
  • How to get inputs using Java Scanner class;
  • Examples with valid, invalid and negative inputs.

That's all folks!

Other Java Tutorials you may like