Open In App

Modulo or Remainder Operator in Java

Last Updated : 23 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Modulo or Remainder Operator returns the remainder of the two numbers after division. If you are provided with two numbers, say A and B, A is the dividend and B is the divisor, A mod B is there a remainder of the division of A and B. Modulo operator is an arithmetical operator which is denoted by %.

NOTE:    If numerator is less than denominator then % will give output as the numerator only.

Syntax:

A % B
Where A is the dividend and B is divisor

Example:

Input : a = 15, b = 6 
// 15%6 means when we divide 15(numerator) by 6(denominator) we get remainder 3// 
Output: 3
Input : a = 16, b = 4 
Output: 0

Approach:

  1. Take Dividend and Divisor from the user.
  2. Create an integer variable and assign it with A % B expression.
  3. Print that variable.

Below is the implementation of the above approach:

Java




// Implementation of Modulo or Remainder Operator in Java
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        // Dividend
        int a = 15;
 
        // Divisor
        int b = 8;
 
        // Mod
        int k = a % b;
        System.out.println(k);
    }
}


Output

7

Time Complexity : 

Modular function usually takes Constant time / O(1) time like other arithmetic operations.
 



Similar Reads

Program to find Quotient And Remainder in Java
The remainder is the integer left over after dividing one integer by another. The quotient is the quantity produced by the division of two numbers. For example, (7/2) = 3 In the above expression 7 is divided by 2, so the quotient is 3 and the remainder is 1. Approach: Divide the dividend by the divisor using / operator. Both dividend and divisor ca
2 min read
BigInteger remainder() Method in Java
The java.math.BigInteger.remainder(BigInteger big) method returns a BigInteger whose value is equal to (this BigInteger % big(BigInteger passed as parameter)).The remainder operation finds the remainder after division of this BigInteger by another BigInteger passed as parameter. Syntax: public BigInteger remainder(BigInteger big) Parameter: The fun
3 min read
BigDecimal remainder() method in Java with Examples
The java.math.BigDecimal.remainder(BigDecimal divisor) is used to calculate the remainder of two BigDecimals. The remainder is given by this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)). This method performs an operation upon the current BigDecimal by which this method is called and the BigDecimal passed as the parameter. Note: T
3 min read
Java Program to Find remainder of array multiplication divided by n
Write a Java program for a given multiple numbers and a number n, the task is to print the remainder after multiplying all the numbers divided by n. Examples: Input: arr[] = {100, 10, 5, 25, 35, 14}, n = 11Output: 9Explanation: 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9Input : arr[] = {100, 10}, n = 5 Output : 0Explanation: 100 x 10 = 1000 % 5
3 min read
Minimum product modulo N possible for any pair from a given range
Given three integers L, R, and N, the task is to find the minimum possible value of (i * j) % N, where L ? i < j ? R. Examples: Input: L = 2020, R = 2040, N = 2019Output: 2Explanation: (2020 * 2021) % 2019 = 2 Input: L = 15, R = 30, N = 15Output: 0Explanation: If one of the elements of the pair is 15, then the product of all such pairs will be d
5 min read
Double colon (::) operator in Java
The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions. The only difference it has from lambda expressions is that this uses direct reference to the method by name instead of providing a delegate t
4 min read
Difference between concat() and + operator in Java
Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created. Concatenation is the process of joining end-
6 min read
&& operator in Java with Examples
&& is a type of Logical Operator and is read as "AND AND" or "Logical AND". This operator is used to perform "logical AND" operation, i.e. the function similar to AND gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensiv
1 min read
|| operator in Java
|| is a type of Logical Operator and is read as "OR OR" or "Logical OR". This operator is used to perform "logical OR" operation, i.e. the function similar to OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is true, i.e. it has a short-circuiting effect. Used extensively to test fo
1 min read
& Operator in Java with Examples
The & operator in Java has two definite functions: As a Relational Operator: & is used as a relational operator to check a conditional statement just like && operator. Both even give the same result, i.e. true if all conditions are true, false if any one condition is false. However, there is a slight difference between them, which h
2 min read
Equality (==) operator in Java with Examples
== operator is a type of Relational Operator in Java used to check for relations of equality. It returns a boolean result after the comparison and is extensively used in looping statements and conditional if-else statements. Syntax: LHS value == RHS value But, while comparing these values, three cases arise generally: Case 1: When both LHS and RHS
4 min read
Diamond operator for Anonymous Inner Class with Examples in Java
Prerequisite: Anonymous Inner Class Diamond Operator: Diamond operator was introduced in Java 7 as a new feature.The main purpose of the diamond operator is to simplify the use of generics when creating an object. It avoids unchecked warnings in a program and makes the program more readable. The diamond operator could not be used with Anonymous inn
2 min read
Java Program To Find Largest Between Three Numbers Using Ternary Operator
Java offers a lot of Operators and one such operator is the Ternary Operator. It is a linear replacement for an if-else statement. Thus, it is a very useful operator and it uses less space in comparison to an if-else statement. Ternary Operator Syntax : variable = condition ? expression1 : expression2 ; The same statement can be written in if-else
2 min read
Java Program to Add Two numbers Without using Arithmetic Operator
Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, –, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the '+' operator and thereby simply printing and displaying out the resultant. But if we r
2 min read
Shift Operator in Java
Operators in Java are used to performing operations on variables and values. Examples of operators: +, -, *, /, >>, <<. Types of operators: Arithmetic Operator,Shift Operator,Relational Operator,Bitwise Operator,Logical Operator,Ternary Operator andAssignment Operator. In this article, we will mainly focus on the Shift Operators in Java
4 min read
Left Shift Operator in Java
The decimal representation of a number is a base-10 number system having only ten states 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. For example, 4, 10, 16, etc. The Binary representation of a number is a base-2 number system having only two states 0 and 1. For example, the binary representation of 4, a base-9 decimal number system is given by 100, 10 as 101
4 min read
Addition and Concatenation Using + Operator in Java
Till now in Java, we were playing with the integral part where we witnessed that the + operator behaves the same way as it was supposed to because the decimal system was getting added up deep down at binary level and the resultant binary number is thrown up at console in the generic decimal system. But geeks even wondered what if we play this + ope
2 min read
new Operator vs newInstance() Method in Java
In Java, new is an operator where newInstance() is a method where both are used for object creation. If we know the type of object to be created then we can use a new operator but if we do not know the type of object to be created in beginning and is passed at runtime, in that case, the newInstance() method is used.In general, the new operator is u
3 min read
instanceof operator vs isInstance() Method in Java
The instanceof operator and isInstance() method both are used for checking the class of the object. But the main difference comes when we want to check the class of objects dynamically then isInstance() method will work. There is no way we can do this by instanceof operator. The isInstance method is equivalent to instanceof operator. The method is
3 min read
new operator in Java
When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. However obtaining objects of a class is a two-step process : Declaration : First, you must declare a variable of the class type. This variable does not define an object. Instead, it is sim
5 min read
Java Ternary Operator Puzzle
Find the output of the program public class GFG { public static void main(String[] args) { char x = 'X'; int i = 0; System.out.print(true ? x : 0); System.out.print(false ? i : x); } } Solution: If you run the program,you found that it prints X88. The first print statement prints X and the second prints 88. The rules for determining the result type
2 min read
Java Ternary Operator with Examples
Operators constitute the basic building block of any programming language. Java provides many types of operators that can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:  Arithmetic OperatorsUna
4 min read
dot(.) Operator in Java
The dot (.) operator is one of the most frequently used operators in Java. It is essential for accessing members of classes and objects, such as methods, fields, and inner classes. This article provides an in-depth look at the dot operator, its uses, and its importance in Java programming. Dot(.) Operator in Java The dot operator is used to access
4 min read
Java Unary Operator with Examples
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: Arithmetic Operators
8 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
C++ | Nested Ternary Operator
Ternary operator also known as conditional operator uses three operands to perform operation. Syntax : op1 ? op2 : op3; Nested Ternary operator: Ternary operator can be nested. A nested ternary operator can have many forms like : a ? b : ca ? b: c ? d : e ? f : g ? h : ia ? b ? c : d : e Let us understand the syntaxes one by one : a ? b : c => T
5 min read
How can we use Comma operator in place of curly braces?
In C and C++, comma (, ) can be used in two contexts: Comma as an operator Comma as a separator But in this article, we will discuss how a comma can be used as curly braces. The curly braces are used to define the body of function and scope of control statements. The opening curly brace ({) indicates starting scope and closing curly brace (}) indic
3 min read
Understanding RxJava Create and fromCallable Operator
In this article, we will learn about the RxJava Create and fromCallable Operators. We can choose between the required function based on what is required skillset is needed. We frequently make mistakes when utilizing RxJava Operators. Let's get this straight so we don't make a mistake. With examples, we shall study the following operations. Createfr
2 min read
RxJava Operator - Concat and Merge
RxJava is the most significant library, and it is widely used by Android developers. It simplifies our lives. RxJava is used for multithreading, managing background processes, and eliminating callback hells. RxJava allows us to address a wide range of complicated use-cases. It allows us to accomplish complex things in a very easy way. It gives us t
3 min read
Multiples of 3 and 5 without using % operator
Write a short program that prints each number from 1 to n on a new line. For each multiple of 3, print "Multiple of 3" instead of the number.For each multiple of 5, print "Multiple of 5" instead of the number.For numbers which are multiples of both 3 and 5, print "Multiple of 3. Multiple of 5." instead of the number. Examples: Input : 15 Output : 1
6 min read
Practice Tags :