Modulo Operator is one of the fundamental operators in Java. It's a binary operator i.e. it requires two operands. In a division operation, the remainder is returned by using the modulo operator. It is denoted by the % (percentage) sign. For example, 5%2 will return 1 because if you divide 5 with 2, the remainder will be 1. For a programmer it's very important to know how to use this operator, they are very important to build logic. For example, in many cases like reversing a number or checking if a number is a palindrome, you can use the modulus operator with 10 to get the last digit, for example, 101%10 will return 1 or 1234%10 will return 4, the last digit. Show It is one of the rather less used arithmetic operators in comparison to +, -, * and /. One of the important points about the remainder operator which is not known by many Java programmers is that it can also be used with floating-point numbers. What is Modulo or Remainder Operator in JavaModulo operator is also known as Remainder operator and denoted by percentage sign (%). It's one of the most basic operators and very useful to create logic in programming, available in almost every single programming language. As we learn in first paragraph, in Java modulus, operator can also be applied to floating-point numbers e.g. 3.0%1.0 is perfectly legal in Java. Since it return remainder value in division operation it is also known as remainder operator.If both operands for %, the modulus operator have type int, then operand_left % operand_right evaluates to the integer remainder. For example, 11 % 3 evaluates to 2 because 11 divided by 3 has a remainder of 2. Also if both operands have type int, the modulus operator (with both operands) evaluates to int. If either or both operands of the mod operator have type double, then evaluating it produces the remainder. This kind of mod operator does not exist in C or C++ where the mod operator only works with int operands. The evaluated result is a double value. For example, 8.27 % 2 evaluates to 0.27 since the division is 4 with a remainder of 0.27. Of course, both values can also be double, so 6.7 % 2.1 evaluates to 0.4 since the division is 3 with a remainder of 0.4. The remainder is computed by assuming the division yields an integer result, with the rest being a remainder How to use Modulo Operator in Java with ExampleYou cannot understand anything better without any example. The same is true for the modulo operators in Java. In this example, we shall show you how to use the modulo operator. The modulo operator is an arithmetic operator that is used to divide one operand by another and return the remainder as its result. You use the modulo operator to get the remainder of the division between an int variable and 10 and a double variable and 10, as described in the code snippet below.import java.util.Arrays; /** * Java program to demonstrate how to use modulo operator in Java. Modulo * operator returns remainder of division operation between two operands. It's * part of Java's arithmetic operator suite. * * @author Javin Paul */ public class Modulo{ public static void main(String args[]) { // Example 1 - Modulo operator returns remainder int iValue = 101; double dValue = 39.02; System.out.println(iValue + " mod 9 = " + iValue % 9); System.out.println(dValue + " mod 9 = " + dValue % 9); // Example 2 - module operator on 10 can give you last digit of integer number int number = 215; int total = 349; System.out.printf("Last digit of %d is %d%n", number, number % 10); System.out.printf("Last digit of %d is %d%n", total, total % 10); // Example 3 - You can use modulo operator on 2 to check if number is even or odd int even = 22; int odd = 21; System.out.printf("%d is %s number%n", even, oddness(even)); System.out.printf("%d is %s number%n", odd, oddness(odd)); } public static String oddness(int i) { return i % 2 == 0 ? "even" : "odd"; } } Output : 101 mod 9 = 2 39.02 mod 9 = 3.020000000000003 Last digit of 215 is 5 Last digit of 349 is 9 22 is even number 21 is odd number Important points about Modulo Operator in JavaNow we know what is modulus or remainder operator, let's revisit some important things about this useful operator :1) It is also known as remainder operator and denoted by percentage sign % 2) You can use the remainder operator to get the last digit of any number e.g. number%10 will give you the last digit, a useful trick to solve many numeric problems. 3) modulus operator is not just applicable to integral types e.g. byte, short, int, long but also to floating-point types like float and double. 4) You can also use the remainder operator to check if a number is even or odd, or if a year is leap year. That's all about how to use the modulo operator in Java. As I said, its one of the important operator and can be very handy in both real-world development as well as solving coding questions during interviews. Apa itu modulo dalam operator aritmatika?Dalam matematika dan dalam pemrograman komputer modulus, operasi modulus adalah sebuah operasi yang menghasilkan sisa pembagian dari suatu bilangan terhadap bilangan lainnya.
Jelaskan apa yang dimaksud dengan operator mod beserta contoh?Operator mod (singkatan dari modulo atau modulus) digunakan untuk menghasilkan angka sisa dari sebuah hasil bagi. Sebagai contoh, 8 mod 5 = 3, karena 3 adalah angka sisa pembagian. Sedangkan 100 mod 7 = 2, karena hanya 98 yang habis dibagi 7 (bersisa 2).
Berapakah hasil dari operasi modulo 25 4?Maka nilai tersebut adalah 5.
|