Jump to content

please solve this

- - - - -

  • Please log in to reply
9 replies to this topic

#1
krishna sowjanya

krishna sowjanya

    Newbie

  • Members
  • Pip
  • 4 posts
:confused:#include<stdio.h>
#include<conio.h>
void main(){
int a;
clr scr();
a=10;
a=a++ + --a;
print f("%d",a);
get ch();
}
the sol must be 21 but its showing 20 http://forum.codecal...1/confused1.gif

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
This is not a tutorial: moved to the correct forum.

Do you understand the difference between a++, ++a, --a, and a--? What exactly are you trying to accomplish?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,083 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

WingedPanther said:

What exactly are you trying to accomplish?
It's probably an assignment, so nothing really :-P

Who said it must be 21?

What'll happen is
a++ + --a
(10 + (10-1) ) + 1... which is 20.

Quote

Do you understand the difference between a++, ++a, --a, and a--?
This is important to know / understand.

#4
krishna sowjanya

krishna sowjanya

    Newbie

  • Members
  • Pip
  • 4 posts
thank you Mr.Panther. if we give the same at print f its giving sol as 21 if u don't mind can u please explain the difference of a++,++a,--a, and . a-- please

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It's a very basic operator in a language, you should know it:
Pre and Post Increment and Decrement Operators
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
krishna sowjanya

krishna sowjanya

    Newbie

  • Members
  • Pip
  • 4 posts
thanx mr oxano but im still in confusion about this solution y bcz when u give in print f statement its giving as 21 can plz explain the difference

#7
krishna sowjanya

krishna sowjanya

    Newbie

  • Members
  • Pip
  • 4 posts
thank you sir i am a bit clarified but i try to understand the logic with your example its really good sol. thanku once again

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Can you post the exact code that produces 21?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
ahshan

ahshan

    Newbie

  • Members
  • Pip
  • 8 posts
code to produce 21


#include<stdio.h>

int main()
{
int a=10;
a=++a + a--; //++a is pre-increment that means it increments the value by 1 before any further computation is done ans a-- is post increment i.e first //current value of a is assigned and then decrements by 1

printf("%d",a);
return 0;
}

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
So really, you're asking what's the difference between:
int a=10;
    a=++a + a--;
and
a=10;
a=a++ + --a;

Correct?

I would expect one to return 22, and the other 20.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users