Let's learn java programming language with easy steps

Sunday, 24 September 2017

Java Programs on Increment and Decrement Operators

Increment and Decrement Operators in Java

Increment and Decrement Operators in java with examples

In this post, We will see some basic java programs on increment and decrement operators.

Let's start, Increment and decrement operators in java with examples.


Program 1

class Test1
{
public static void main(String args[])
{
int a, b;
a = 30;
b = ++a;
System.out.println(b);
}
}

Output: 31


Program 2

class Test2
{
public static void main(String args[])
{
int a,b;
a = 30;
b = a++;
System.out.println(b);
System.out.println(a);
}
}

Output: 30
              31


Program 3

class Test3
{
public static void main(String args[])
{
int a = 40;
a = ++a + 2;
System.out.println(a);
}
}

Output: 43


Program 4


class Test4
{
public static void main(String args[])
{
int a = 50;

a = a++ + 1;
System.out.println(a);
}
}

Output: 51


Program 5

class Test5
{
public static void main(String args[])
{
int x = 10;
x = ++x + ++x;
System.out.println(x);
}
}

Output: 23


Program 6

class Test6
{
public static void main(String args[])
{
int a = 30;
a = a++ + a++;
System.out.println(a);
}
}

Output: 61


Program 7

class Test7
{
public static void main(String args[])
{
int x = 5;
int y = 21;
int z = ++x *7 + 2 - y--;
System.out.println("z = "+z);
}
}

Output: 23


Program 8

class Test8
{
public static void main(String args[])
{
int a = 30;
a = a++ + ++a;
System.out.println(a);
}
}

Output: 62


Program 9

class Test9
{
public static void main(String args[])
{
int a,b;
a = 50;
b = --a;
System.out.println(b);
}
}

Output: 49


Program 10

class Test10
{
public static void main(String args[])
{
int x,y;
x = 40;
y = x--;
System.out.println(x);
}
}

Output: 39


Program 11

class Test11
{
public static void main(String args[])
{
int z = 40;
z = z-- + ++z;
System.out.println(z);
}
}

Output: 80


Program 12

class Test
{
public static void main(String args[])
{
int x = 1;
System.out.println(a++);
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
}
}

Output: 1
              2
              4
              4
              2

Visit below links for more practice 

Java Array Programming Interview Questions.
Java String Programming Interview Questions.


Above examples are basic increment and decrement operator examples in java.
Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate