a = 2;
b = 3;
a = (a + b) % 2;
b = b – a;
a = a * b;
cout << a << "\n" << b ;

What is the output for a and b?
------------------------------------------------------------
Why is a 3 and b 2 is this one

a = 2;

b = 3;

a = a + b;

b = a – b;

a = a – b;

cout <<a<<endl;

cout << b;

can anyone offer some assitance in explainning?

>What is the output for a and b?
Run it and see.

>Why is a 3 and b 2 is this one
2 + 3 = 5
5 - 3 = 2
5 - 2 = 3

yea i ran it but got an error so i dont know

is the answer 3, 2?

>yea i ran it but got an error so i dont know
You probably got the same error I did. It's hard to see, but – and - are not the same character. If that doesn't fix your problem, post the errors you're getting and all of the code.

i got a=3 and b=2 is that right?

You have two separate pieces of code. Which one are you asking about?

This one?

a = (a + b) % 2;
b = b – a;
a = a * b;

or this one?

a = a + b;
b = a – b;
a = a – b;

And do you still have the error?

the first one

I get a = 2 and b = 2.

hmmm...can you explain how you got that?

By that do you mean:

1) Your program doesn't run still?
2) Your program runs and gives you a different answer?
3) Your program and mine give the same results, but the answer seems wrong to you?


If it's 1 or 2, please post your full program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.