Adding incrementing or subtracting decrementing the value on

Adding (incrementing) or subtracting (decrementing) the value one from an integer variable is a common, everyday operation. To increment an int variable x, we could code x = x + 1; As an alternative, we could use the special operators ++ and -- to increment and decrement a variable. Use the first method to increment x in the program below. Print the value of x after incrementing. Use the ++ operator to increment in the program below. Print the value of y after incrementing. public class Increment Demo public static void main(String[] args) {int x = 10: int y = 3;//Put your code here}}

Solution


/**
*
* The java progam IncrementDemo that demonstrates
* the increment oeprator ++ for x and y values and
* print results to console.*/
//IncrementDemo.java
public class IncrementDemo {  
   public static void main(String[] args) {
      
      
       //initialize x and y values
       int x=10;
       int y=-3;
      
       System.out.println(\"Before incrementing\");
       //print before incrementing x and y values
       System.out.println(\"x= \"+x+\" y =\"+y);
      
      
       System.out.println(\"After incrementing\");      
       //increment of x
       x++;//x=x+1=10+1=11
       System.out.println(\"x =\"+x);
      
       y++;//y=y+1=-3+1=-2      
       System.out.println(\"y =\"+y);
      
   }

}

----------------------------

Sample output:

Before incrementing
x= 10 y =-3
After incrementing
x =11
y =-2

 Adding (incrementing) or subtracting (decrementing) the value one from an integer variable is a common, everyday operation. To increment an int variable x, we

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site