Java: modulus

Author: Ronald S. Holland
at
Total Application Works

ronholland@sumtotalz.com





To contact us



HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2016 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <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


Read Me First

The JAV_Modulus.zip file contains the following files:

  1. JAV_Modulus.zip - This file that contains the following files:
  2. JAV_Modulus.java - this file contains the main method code and is the entry point when the program is loaded into memory,
  3. JAV_Modulus2.java - this file contains the main method code and is the entry point when the program is loaded into memory,
  4. JAV_Modulus.html - is the tutorial that describes how to build the JAV_Modulus and JAV_Modulus2 class programs
  5. CompileandRunModulus.bat - When clicked, the file will compile and run the Modulus.java file.
  6. CompileandRunModulus2.bat - When clicked, the file will compile and run the Modulus2.java file.
This application requires Microsoft c++ Visual Studio to run.

To run any of the projects:

  1. Go to the folder where you downloaded the CPP_Inheritance.zip compressed file
  2. Extract the files
  3. Right clck on CPP_Student.vcproj - click on the version of Microsoft studio you have
  4. When Microsoft studio IDE is started
  5. Click on the Debug menu
  6. Click on the Start without Debugging menu
    • the CPP_Student project will begin to execute

Introduction

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.

Java modulus operator

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.

 

  1. 5 % 2 = 1
  2. 99 % 3 = 0
  3. 145 % 3 = 1
  4. 9 % 7 = 2
  5. 12 % 7 = 5
  6. 33 % 13 = 7
  7. 45 % 9 = 0
  8. 25 % 7 = 4
  9. 17 % 13 = 4
Figure 1: Examples using the modulus function


In Figure 1, we show some examples using the modulus function. There are several simple problems that divide one number by another: what's left over when we complete the division is the remainder. The answer is easy to compute: divide 11 by 4 and take the remainder: 3. How can we compute this in a programming language like C or Java? The Java language provides a built-in function, the modulus operator ('%'), that provides a remainder that results from performing integer division. Some of the uses for the modulus function are discussed in the next section.

The modulus operator is useful in a variety of circumstances

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


In Figure 2, we show in a random roll of a die, the frequency of the numbers rolled. In the next example, we take two numbers entered by the user and then compute the remainder.

 
/** **************************************************
 * 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


Conclusions

The purpose of this tutorial was to:

  • Introduce you to the modulus function by
    • Showing some examples using modulus operator
    • Showing how the modulus operator is useful in a variety of circumstances
  • Giving you fundamentals of the modulus operator so that you can confidently use this operator on your own.





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