Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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.

    Please give me the detail description about this.

    Thank You.

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    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

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for your reply chris0.
    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.

    Please give me the detail description about this problem.

    thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •