PDA

View Full Version : Arithmetic Operation Problem

ashishchaudhary
09-24-2011, 09:32 AM
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.

chris0
09-24-2011, 07:03 PM
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

ashishchaudhary
09-25-2011, 08:11 AM
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.