what is the way to Calculate

• April 3rd, 2013, 10:46 AM
what is the way to Calculate
public class NewClass4 {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
• April 3rd, 2013, 10:47 AM
Norm
Re: what is the way to Calculate
Do you have any specific questions about the code?

[code=java]
[/code]
to get highlighting and preserve formatting.
• April 3rd, 2013, 11:14 AM
Starstreak
Re: what is the way to Calculate
How does c = 3?

For clarity, rewrite the code without using shorthand (augmented operators).
Code :

``` int a = 1; int b = 2; int c = 3; a = a + 5; b = b * 4; c = c + a * b; c = c % 6; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c);```
Then trace values of all the variables on some paper.

When you get to c = c + a * b; note that multiplication, division, and remainder operators are applied first.
This means a*b is evaluated first and then added to c.
• April 3rd, 2013, 11:22 AM
Re: what is the way to Calculate
ok but how i do it a*b is 2 and then how do i do c=c&6
• April 3rd, 2013, 12:00 PM
Starstreak
Re: what is the way to Calculate
The % sign means divide the two numbers and give us only the remainder.
• April 3rd, 2013, 12:58 PM
Re: what is the way to Calculate
so what the result of c = c + a * b;

--- Update ---

what the result of c = c + a * b;
• April 3rd, 2013, 01:04 PM
Norm
Re: what is the way to Calculate
Do you have a PC and a java compiler? Write a small program with that statement in it, compile it and execute it.
Make sure the values of the variables are not the same and are greater than 1.
• April 8th, 2013, 12:29 PM
hope_08
Re: what is the way to Calculate
hi

well if we trace the code then
a = 1+5 =6;
b = 2*4 = 8;
c = 3 + 48 =51;
c = 51%6 = 3 (since dividing 51 by 6 gives remainder 3)