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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/classes_objects -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/class_methods -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/exceptions -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/modifiers -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/oop -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/packages_api -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/user_input -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/wrapper_classes -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/identifier -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/arrays -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/comments -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/data_types -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/operators -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/output -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/strings -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/variables -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} java/system_methods -.-> lab-117908{{"`Java Long remainderUnsigned Method`"}} end

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