# Math and Comparison Operators

### Basic Math Operators

You've already used `+` and `/` to add and divide. Java also includes `-` and `*` for subtraction and multiplication. In addition, there's another basic math operator `%` (it's called mod, and it uses the percent symbol) which is used to get the remainder after division. For example, `5 % 2` will return 1 (the remainder of 5 / 2).

Order of Operations
When you combine multiple operations in one line, Java is similar to the standard order of operations (PEMDAS):

1. Parentheses
2. Multiplication and Division and Mod
3. Addition and Subtraction

Operations on the same level will be evaluated in order from left to right, just like the standard convention.

### Comparison Operators

We've seen that `=` assigns a value and shouldn't be confused with `==` which checks if two values are equal. These are all the ways to compare values:

Start with `int a = 5;`

symbol meaning true example
== equal to a == 5;
!= not equal to a != 3;
> greater than a > 3;
>= greater than or equal to a >= 5;
< less than a < 6
<= less than or equal to a <= 5;

### All Order of Operations

This table summarizes the order of all the operations. The higher an operation appears, the higher its precedence. (You can view the full table on the Java Tutorials.)

Name Symbol
not `!`
parenthetical `( )`
multiplicative `* / %`
additive `+ -`
relational `< > <= >= `
equality `== !=`
logical AND* `&&`
logical OR* `||`
assignment `=`

*The logical operators will be covered soon.

General Tip
If you need to reference some Java quickly while learning this module, you can click on the button on the sidebar to see sample Java code.

### Challenge

Given two numbers `a` and `b`, return the remainder when their product is divided by their sum.

For example, if given `2` and `5`, return `3` (since their product `10` divided by their sum `7` leaves a remainder of `3`).

Alternatively, you can try out Learneroo before signing up.

### Challenge

What is the value of `num` after the following code?
`int num = 15 / (1 + 2) % 3;`

Alternatively, you can try out Learneroo before signing up.

• #### Jay Learn

I solved this problem with first declaring sum and mul as integers. Then returning their values using modulus.

• #### Ryan

the question confused me, when did i have to multiply at any point???

• #### Learneroo

@Ryan, the product of `a` and `b` means multiply `a` by `b`. Would this be clearer:

Multiply `a` by `b` and then get the remainder from dividing that product by the sum of `a` and `b` ?

• #### Ryan

Yeah, thank you. The product made me think addition but i guess that would be the sum. Maybe i should have taken more math classes lol. So far this has been the best teaching program i have used

• #### Brent Jones

Excellent tutorial thus far. It is probably hard to understand the precise syntax that is needed to pose the problems.

• #### Denis Semendyayev

i was so confused by the wording of product. to me it processed as the same thing as sum :/

• #### Matt

I read the challenge about 5 times and operated exactly what they wrote: return (a*b)%(a+b);

I hope this helps. Wording can be tricky but it's all going back to basic math terminology.

• The remainder of 5 % 3 could have been -1 too.

• #### ArtTric

This one tripped me too. It can be done this way as well:
int product = a * b;
int sum = a + b;
return product % sum;

• #### Mitchell

PEMDAS is also known as BEDMAS (brackets instead of parentheses)

• #### thales

I am a math teacher so this wasnt to hard for me :-)

• #### Joseph Estridge

I have a question... I understand that you can just return the value of "(a * b) % (a + b);" ... But I also tried doing this one by assigning to value to a new variable, int C. I added an int

• #### Eze Sunday Eze

i got confused, i got it when i read the question again.

• Good way of learning..

• #### Shandeep Kumar

that's really awsome man

• #### Roberto Blanco Axelsson

My solutions was like ArtTric

• #### Enoch

I don't know where to input int product = a*b;
Int sum = a+b;

• #### paul. kirsch

I got it correct after 3 tries. I may be wrong but I surmise that '%'is to be put between code that identifies what variables you are working with such as 'sum' and really means: "divide and also give me the remainder".

• #### hunter

yeeyee

• #### Kurt

something is wrong here because how is 5*1 divided by (5+1) have a remainder of 5?

basic math says its .8 with a remainder of 2. so 5*1 is 5. well 5 divided by 6 equals 0.8 r2

• #### Kurt

I get the code, but it has a flaw because 5*1 divided by 6 does not have a remainder of 5. its 2

because basic math says that 5/6 is 0.8 with a remainder of 2.

• #### lucifer

guys i did this
//int num
//(1*5)%(6);
return (a*b)%(a+b)

• #### noel

int mul = a * b;
int sum = a+b;
int result = mul%sum;
return result;

• #### Santo Credendino

What is going on in this solution:
Applying the rules for Math and Operator Order of Precedence
1. addition is performed first because of ( )
2. multiplication is the next operation

• #### Raj

it can be also done by this:
int product = a+b;
return (a*b)%product;
// product is the sum of A and B, i could have named it anything//