write a program to demonstrate the use of the shorthand assignment operator using scanner class and object
Answers
Answer:
Before Creating a Program to demonstrate the Use of Shorthand Operator, You Must Understand the Meaning of it.
Let's Take a Look at its Definition and more.
Java has a shorthand operator for these kinds of assignment statements
+= Addition assignment x += 4; x = x + 4;
-= Subtraction assignment x -= 4; x = x - 4;
*= Multiplication assignment x *= 4; x = x * 4;
/= Division assignment x /= 4; x = x / 4;
%= Remainder assignment x %= 4; x = x % 4;
Let's Us Have an Example on The Same Operator which demonstrate the Square of the Number Entered by the User.
import java.util.Scanner;
class program{ // You Can Name Your Class Accordingly.
public static void main(String []args){
Scanner sc = new Scanner(System.in); // Creating the Scanner Class Object.
int n;
System.out.println("Enter an Integer:");
n = sc.nextInt();
n*=n; // Here we are Updating the n Value by itself. So it is definitely an Example of Shorthand Operator
System.out.println("The Square of your Number is: "+n);
}
}