Arithmetic assignment operations

  1. Combine arithmetic and assignment into a single operation to save coding time
  2. All five basic arithmetic operations have a corresponding arithmetic assignment operator .
Operator
Operation
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulo (remainder) assignment
Example:
This program reads a percentage value from the user (such as 5.5) and converts to the correct decimal equivalent (such as 0.055) by using the division assignment operator.
import java.util.*;
import java.io.*;

public class AppStr
{
public static void main(String[] args)
{

// Variable for holding a percentage entered by the user // Prompt for and read the percentage System.out.print(“Enter a percentage in n.nn format: “);
double percent;
Scanner Keyboard = new Scanner(System.in);

percent = Keyboard.nextDouble();
//percent = Keyboard.readDouble();
// Convert it to its decimal equivalent and display the result
percent /= 100;
System.out.println(“The decimal equivalent is ” + percent);
}
}

Scroll to Top