Monday, 5 January 2015

Java Program to test Increment Decrement Operations


This is a Java program to test increment and decrement operators.
Increment operator increases the value by 1.
Decrement operator decreases by 1.

There are two types of increment and decrement operators ( pre and post)

++a  preincremeent (first increments the value for processing)
a++ postincrement ( first time same value of a is used , then for after use it increments by 1)
--a preincrement (first decrements the value for processing)
a-- postincrement( first time same value of a is used , then for after use it decrements by 1)


PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class IncrementDecrement {
 public static void main(String[] args) {
  int number, postdecrement, preincrement, predecrement, postincrement;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter any number :\t");
  number = scan.nextInt();
  scan.close();

  preincrement = ++number;
  predecrement = --number;
  postincrement = number++;
  postdecrement = number--;

  System.out.println("Preincrement is " + preincrement);
  System.out.println("Predecrement  is " + predecrement);
  System.out.println("Postincrement  is " + postincrement);
  System.out.println("Postdecrement  is " + postdecrement);
  System.out.println("The value of original number is " + number);
 }
}


OUTPUT :

No comments:

Post a Comment