Monday 8 June 2015

Arithmetic Operators

public class ArithmeticOperators {
        /* Arithmetic operators offered by the Java language are;
         * + Addition.
         * - Subtraction.
         * * Multiplication.
         * / Division.
         * % Remainder.
         */
        public static void main(String[] args) {
                int result = 1 + 2;
                // The result is now 3.
                System.out.println("1 + 2 = " + result);
                int original_result = result;

                result = result - 1;
                // The result is now 2.
                System.out.println(original_result + " - 1 = " + result);
                original_result = result;

                result = result * 2;
                // The result is now 4.
                System.out.println(original_result + " * 2 = " + result);
                original_result = result;

                result = result / 2;
                // The result is now 2.
                System.out.println(original_result + " / 2 = " + result);
                original_result = result;

                result = result + 8;
                // The result is now 10;
                System.out.println(original_result + " + 8 = " + result);
                original_result = result;

                result = result % 7;
                // The result is now 3.
                System.out.println(original_result + " % 7 = " + result);
        }
}

This example was lifted pretty much "as is" from the Oracle Java tutorials.

No comments:

Post a Comment

Translate