Java: modulus


HOME]  Consulting  Design  Maintenance  Project  Testing  Training  Turnkey  Java  C++  SQL  HTML  JavaScript  C# 
© 2002  2016 All Rights Reserved Total Application Works
<html> <head> <title>Example of a link </title> </head> <body> <a href="http://sumtotalz.com/TotalAppsWorks/JavaJAV_Modulus.html"> Programming Tutorials: modulus </a> </table> </body> </html> 

Download source 
The JAV_Modulus.zip file contains the following files:
To run any of the projects:
This tutorial assumes that you have some prior programming experience. It is not intended to be a comprehensive discussion on Java or programming. In this tutorial, we will briefly discuss variables, as we ease into Java. Java as a programming language is widespread and can be found on nearly all computers.
What is the modulus operator? The modulo operation finds the remainder of division of one number by another number e.g., 6/5 = 1.
Consider two positive numbers, where a is the dividend and n is the divisor e.g., a/n, a modulo n (abbreviated as a mod n = b), where b is the remainder. For example, the expression "9 mod 8" would evaluate to 1 because 9 divided by 8 results in a remainder of 1,
whereas, "4 mod 2" results in a remainder of 0. Consider the examples in Figure 1 below.

Figure 1: Examples using the modulus function 

How is the modulus operator is useful? It is commonly
used to take a randomly generated number and reduce that number to a random
number on a smaller range. For example, the following Figure 3 shows the remainder when a number is devided by another number and
whether the numbers from 0 to 9 are odd or even.
/** ************************************************** * Modulus.java  this program shows some examples * using Modulus operator. *****************************************************/ import javax.swing.JOptionPane ; class JAV_Modulus { public static void main( String args[] ) { // System.out.println( "\nWhen 1020 is divided by 91, the remainder is " + (1020 % 91 ) ); // System.out.println( "When 100 is divided by 91, the remainder is " + (100 % 91) ) ; // System.out.println( "When 85 is divided by 80, the remainder is " + (85 % 80 ) ) ; // System.out.println( "When 5 is divided by 5, the remainder is " + (5 % 5 ) ) ; for (int i = 0; i < 10; i++) { if (i % 2 == 0) System.out.println( i + " is even."); else System.out.println( i + " is odd."); } } // end Main } // end class Modulus******************/ 
Output 

When 1020 % 91, the remainder is 19 When 100 % 91, the remainder is 9 When 85 % 80, the remainder is 5 When 5 % 5, the remainder is 0 0 is even. 1 is odd. 2 is even. 3 is odd. 4 is even. 5 is odd. 6 is even. 7 is odd. 8 is even. 9 is odd. 
Figure 2: Modulo operator 
/** ************************************************** * Modulus2.java  this program shows some examples * using Modulus operator. *****************************************************/ import javax.swing.JOptionPane ; class JAV_Modulus2 { public JAV_Modulus2() { int num, num2, num3; String firstNum = "", // first string entered by user secondNum = "" ; // second string entered by user do { firstNum = JOptionPane.showInputDialog( "Enter an integer dividend" ) ; num = Integer.parseInt( firstNum ) ; secondNum = JOptionPane.showInputDialog( "Enter an integer divisor" ); num2 = Integer.parseInt( secondNum ) ; System.out.println( num + " % " + num2 + " equals " + (num % num2) ) ; firstNum = JOptionPane.showInputDialog( "Enter an 1 to quit" ) ; num3 = Integer.parseInt( firstNum ) ; if (num == 1) break; } while (num3 != 1); } public static void main( String args[] ) { JAV_Modulus2 app = new JAV_Modulus2() ; } // end Main } // ..................... end class Modulus 
Output 

Enter an integer dividend 55 Enter an integer divisor 7 55 % 7 equals 6 Enter an 1 to quit 
Figure 3: Example II using the modulus function 
The purpose of this tutorial was to:
HOME]
Consulting
Design
Maintenance
Project
Testing
Training
Turnkey
Java
C++
SQL
HTML
JavaScript
C#
To contact us
© 2002  2016 All Rights Reserved Total Application Works