Signup/Sign In
PUBLISHED ON: APRIL 22, 2021

Java Program to Perform Assignment Operations

In this tutorial, we will learn how to perform assignment operations by taking input from the user. But before moving further, if you are not familiar with the concept of the assignment operator in java, then do check the article on Operators in Java.

Input: num1=10

num2=20

Output:

num1+=num2

Value of num1=30

num -=num2

Value of num1=10

Two cases arise for the above problem:

Case 1: When values are pre-defined

Case 2: When values are user-defined

Let us look at each of these cases separately.

Program 1: To Perform the Assignment Operations

In this program, we will perform the assignment operations when the values are pre-defined in the program.

Algorithm:

  1. Start
  2. Here, we will use a switch case to choose from different assignment operators like +=, -=, *=, /=, and %=.
  3. Declare two variables.
  4. Initialize it.
  5. Perform all the assignment operators like +=, -=, *=, /=, and %=.
  6. Display the result of each assignment operation.
  7. Stop.

Below is the code for the same.

//Java Program to perform Assignment Operator
import java.io.*; 
  
public class Main
{ 
    public static void main(String[] args) 
    { 
  
        // Declaring variables 
        int num1 = 10, num2 = 20; 
        int res;
        System.out.println("Initial value of num1 = " + num1); 
        System.out.println("Initial value of num2 = " + num2); 
        // Adding & Assigning values 
        num1 += num2; 
        // Displaying the assigned values 
        System.out.println("Value of num1 after += is " + num1);
        // Subtracting & Assigning values 
        num1 -= num2; 
        // Displaying the assigned values 
        System.out.println("Value of num1 after -= is " + num1);
        // Multiplying & Assigning values 
        num1 *= num2; 
        // Displaying the assigned values 
        System.out.println("Value of num1 after *= is " + num1);
        // Dividing & Assigning values 
        num1 /= num2; 
        // Displaying the assigned values 
        System.out.println("Value of num1 after /= is " + num1);
        // Moduling & Assigning values 
        num1 %= num2; 
        // Displaying the assigned values 
        System.out.println("Value of num1 after %= is " + num1); 
    } 
} 


Initial value of num1 = 10
Initial value of num2 = 20
Value of num1 after += is 30
Value of num1 after -= is 10
Value of num1 after *= is 200
Value of num1 after /= is 10
Value of num1 after %= is 0

Program 2: To Perform the Assignment Operations

In this program, we will see how to perform assignment operations in java when the values are user-defined. Here, firstly we will ask the user to input the values, and then we will perform the assignment operations

Algorithm:

  1. Start
  2. Here, we will use a switch case to choose from different assignment operators like +=, -=, *=, /=, and %=.
  3. Declare a variable for the same.
  4. Ask the user to initialize it.
  5. Based on the operation chosen, declare two variables.
  6. Ask the user to initialize the variables.
  7. Display the result after performing the assignment operations.
  8. Stop.

Below is the code for the same.

//Java Program to perform Assignment Operator
import java.util.*; 
  
public class Main
{ 
    public static void main(String[] args) 
    { 
        //Take input from the user
      //Create instance of the Scanner Class
      Scanner s=new Scanner(System.in);
       while(true)
        {
            System.out.println("");
            
            System.out.println("Choose the operation you want to perform ");
            System.out.println("Choose 1 for += ");
            System.out.println("Choose 2 for -= ");
            System.out.println("Choose 3 for *= ");
            System.out.println("Choose 4 for /= ");
            System.out.println("Choose 5 for %= ");
            System.out.println("Choose 6 for EXIT");
           
            int n = s.nextInt();
            switch(n)
            {
                case 1:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int x = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int y = s.nextInt();
                    System.out.println("Initial value of x is " + x);
                    x+=y;
                    System.out.println("The value of x after += is " + x);
                    break;
 
                case 2:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int p = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int q = s.nextInt();
                    System.out.println("Initial value of p is " + p);
                    p-=q;
                    System.out.println("The value of p after -= is " + p);
                    break;
 
                case 3:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int a = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int b = s.nextInt();
                    System.out.println("The initial value of a is " + a);
                    a*=b;
                    System.out.println("The value of a after *= is " + a); 
                    break;
 
                case 4:
                     System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int c = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int d = s.nextInt();
                    System.out.println("Initial value of c is " + c);
                    c/=d;
                    System.out.println("The value of c after /= is " + c); 
                    break;
                case 5:
                     System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int e = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int f = s.nextInt();
                    System.out.println("Initial value of e is " + e);
                    e%=f;
                    System.out.println("The value of e after %= is " + e); 
                    break;
                case 6:
                    System.exit(0);
            }
        }
    } 
} 


Choose the operation you want to perform
Choose 1 for +=
Choose 2 for -=
Choose 3 for *=
Choose 4 for /=
Choose 5 for %=
Choose 6 for EXIT
1
Enter the two numbers to perform operations
Enter the first number:
Enter the second number:
The initial value of x is 5
Value of x after += is 9

Choose the operation you want to perform
Choose 1 for +=
Choose 2 for -=
Choose 3 for *=
Choose 4 for /=
Choose 5 for %=
Choose 6 for EXIT
2
Enter the two numbers to perform operations
Enter the first number:
Enter the second number:
The initial value of p is 4
Value of p after -= is 1

Choose the operation you want to perform
Choose 1 for +=
Choose 2 for -=
Choose 3 for *=
Choose 4 for /=
Choose 5 for %=
Choose 6 for EXIT
3
Enter the two numbers to perform operations
Enter the first number:
Enter the second number:
The initial value of a is 4
The value of a after *= is 20

Choose the operation you want to perform
Choose 1 for +=
Choose 2 for -=
Choose 3 for *=
Choose 4 for /=
Choose 5 for %=
Choose 6 for EXIT
4
Enter the two numbers to perform operations
Enter the first number :
Enter the second number :
The initial value of c is 8
Value of c after /= is 1

Choose the operation you want to perform
Choose 1 for +=
Choose 2 for -=
Choose 3 for *=
Choose 4 for /=
Choose 5 for %=
Choose 6 for EXIT
5
Enter the two numbers to perform operations
Enter the first number :
Enter the second number :
The initial value of e is 7
Value of e after %= is 1

Choose the operation you want to perform
Choose 1 for +=
Choose 2 for -=
Choose 3 for *=
Choose 4 for /=
Choose 5 for %=
Choose 6 for EXIT
6



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.