Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Arithmetic Operation Problem

hello Every one,
Today my problem is much complex for me, I am so confused why is this happening;
The problem is
if i have a = 2;

********** In C Language **********
and i perform

i = a++ + ++a + --a + a-- + ++a;
then result is = 15

i = a++ + ++a + --a + a--;
then result is = 8

************ In Java Language ************

i = a++ + ++a + --a + a-- + ++a;
then result is = 15

i = a++ + ++a + --a + a--;
then result is = 12

I don't know why is this happening, what is the reasion, I am performing the same operation but the result is different.

Thank You.

2. thats interseting,
make sure you that the initial value of a is the same in java and in c

try to divide it with brackets

i = (a++) + (++a) + (--a) + (a--);

and see if that makes a difference

But the problem is same as it is yet.

for the java the program is

public class test {

public static void main(String ar[]) {

int a=2,j=0,k=0;

j = a++ + ++a + --a + a-- + ++a;
k = a++ + ++a + --a + a--;

System.out.println("The value of j = " + j + " and the value of k = " + k);

}

}

then the result is
The value of j = 15 and the value of k = 12
Also if i seperate all of them with parenthesis like you told (a++) then the
result is same.

for c the program is

int main(void)
{

int a=2,j=0,k=0;
clrscr();

j = a++ + ++a + --a + a-- + ++a;
k = a++ + ++a + --a + a--;

printf("The value of j = %d and the value of k = %d", j,k);

getch();
return 0;

}

then the result is
The value of j = 15 and the value of k =8
I used parenthesis Alos, but nothing happened.