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);
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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);
Last edited by ohad; April 3rd, 2013 at 09:47 AM. Reason: What is the way to Calculate (C)how the result are 3?
Do you have any specific questions about the code?
Please edit your post and wrap your code with code tags:
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
How does c = 3?
For clarity, rewrite the code without using shorthand (augmented operators).
Then trace values of all the variables on some paper.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);
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.
ok but how i do it a*b is 2 and then how do i do c=c&6
The % sign means divide the two numbers and give us only the remainder.
so what the result of c = c + a * b;
--- Update ---
what the result of c = c + a * b;
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.
If you don't understand my answer, don't ignore it, ask a question.
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)