Ad Code

Responsive Advertisement

ARITHMETIC OPERATORS

 

Java Operators with Precedence 11 and 12



Precedence

Operator

Type

Associativity

11

+

Addition

 

 Left to Right

-

Subtraction

12

*

Multiplication

/

Division

%

Modulus

All the basic Arithmetic operations are involved with the precedence 11 and 12. The higher value of precedence depicts the highest priority. To know more about the precedence of operators, CHECK HERE.

You might have heard about the BODMAS rule in Mathematics which determines which operation has to be done first. Similarly, since Java has multiple operators this precedence table is used to rank those operators. If all these operators are present in a single line, then the order of the operation occurs in the following manner:

Modulus > Division > Multiplication > Addition > Subtraction

Eg) 30- 3 + 265 % 7 /6 *30

Even though the subtraction operator is present at first, based on the precedence value ‘%’ is taken first. Modulus operation returns the reminder of the division process in output. So 265 % 7 results in the output 6. Then the division operation is given the next priority and 6/6 becomes 1. Then multiplication operation takes place and results in

30. Then 30 is added with 30 (first element) and finally subtracted with 3. Thus the output becomes 57. This process can be implemented using a simple Java code.

        class operator{
            public static void main(String args[]){
                System.out.println("The output is "+ (30- 3 + 265 % 7 /6 *30 ) );
                System.out.println("The output is "+ (2- 3 + 4) );
                System.out.println("The output is "+ (1129 % 30/45) );
               }
        }

 Output

Try implementing the following scenario using Java code:

A teacher wants to calculate the Average mark of 2 students and declare whether they have passed or failed in the examination. If the average is greater than 35, the student passes else fails. The marks of student 1 is 29, 13, 7, 40 and 47. The marks obtained by the student 2 is 97, 86, 89, 76 and 100. Calculate the average marks of the students and declare whether the student passed or failed in the examination.

class operator{
    public static void main(String args[]){
        // Storing the average marks of student 1 in variable 'a'
        int a = (29 + 13 + 7 + 40 + 47)/5;
        // Storing the average marks of student 2 in variable 'b'
        int b = (97 + 86 + 89 + 76 + 100)/ 5;
        //Declaring whether the student passed or failed
        //Using Ternary Conditional operator
        System.out.println(a>35 ? "Student 1 passed" : "Student 1 failed");
        System.out.println(b>35 ? "Student 2 passed" : "Student 2 failed");
    }
}

 

If you want to have more clarity about Ternary conditional operator, you can CHECK HERE.

Output



                                                                                                                                            NEXT



Post a Comment

2 Comments