Friday, 30 December 2016

Arithmetic Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class ArithmeticOperatorsExample {

  public static void arithmeticOperators() {

    int a = 10;
    int b = 5;
    int result;

    // + Addition example
    result = a + b;
    System.out.println("Addition of two number is :: " + result);

    // - Subtraction example
    result = a - b;
    System.out.println("Subtraction of two number is :: " + result);

    // * Multiplication example
    result = a * b;
    System.out.println("Multiplication of two number is :: " + result);

    // / Division example
    result = a / b;
    System.out.println("Division of two number is :: " + result);

    // % Remainder example
    result = a % b;
    System.out.println("Remainder of two number is :: " + result);

    // ++ Post Increment example
    result = a++;
    System.out.println("Post Increment of a is :: " + result);
    // ++ Pre Increment example
    a = 10//reset value of a
    result = ++a;
    System.out.println("Pre Increment of a is :: " + result);

    // -- Post Decrement example
    result = b--;
    System.out.println("Post Decrement of b is :: " + result);
    // -- Pre Decrement example
    b = 5//reset value of b
    result = --b;
    System.out.println("Pre Decrement of b is :: " + result);
  }

  public static void main(String[] args) {
    
    ArithmeticOperatorsExample.arithmeticOperators();
  }

}

Output →

Addition of two number is :: 15
Subtraction of two number is :: 5
Multiplication of two number is :: 50
Division of two number is :: 2
Remainder of two number is :: 0
Post Increment of a is :: 10
Pre Increment of a is :: 11
Post Decrement of b is :: 5
Pre Decrement of b is :: 4

No comments:

Post a Comment