This is a Java Program to Illustrate Use of Pre and Post Increment and Decrement Operators.The increment (++) operator increases its operand by one. The decrement (–) operator decreases its operand by one.
Enter an integer as input. After that we perform the pre and post increment and decrement operators on the given integer and hence get the respective ouput.
Here is the source code of the Java Program to Illustrate Use of Pre and Post Increment and Decrement Operators.The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Increment_Decrement
{
public static void main(String[] args)
{
int a, b, c, d, e;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer a:");
a = s.nextInt();
b = ++a;
System.out.println("Result after Pre Increment a:"+a);
System.out.println("Result after Pre Increment b:"+b);
c = a++;
System.out.println("Result after Pre Increment a:"+a);
System.out.println("Result after Post Increment c:"+c);
d = --a;
System.out.println("Result after Pre Increment a:"+a);
System.out.println("Result after Pre Decrement d:"+d);
e = a--;
System.out.println("Result after Pre Increment a:"+a);
System.out.println("Result after Post Decrement e:"+e);
}
}
Output:
$ javac Increment_Decrement.java $ java Increment_Decrement Enter any integer a:12 Result after Pre Increment a:13 Result after Pre Increment b:13 Result after Pre Increment a:14 Result after Post Increment c:13 Result after Pre Increment a:13 Result after Pre Decrement d:13 Result after Pre Increment a:12 Result after Post Decrement e:13
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Java Internship
- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Practice Information Technology MCQs