programme for swapping of two numbers without using temporary varaible

Discussion in 'C++' started by cindrilla, Nov 21, 2010.

?

was this helpful?

  1. good

    33.3%
  2. helpful

    0 vote(s)
    0.0%
  3. veryhelpful

    66.7%
  4. not so

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. cindrilla

    cindrilla New Member

    Joined:
    Sep 26, 2010
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    hyderabad
    hi frinds lets learn a programme to swap two numbers withouit using temporary variable .
    this is in "c"
    Code:
    /*swapping of two numbers*/
    
    #include<stdio.h>
    main()
    {
    int a,b;
    printf("enter the values of a & b");
    scanf("%d %d",&a,&b);
    printf("the values before swapping are %d,%d",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("now the values after swapping are %d,%d",a,b);
    }
     
    Last edited by a moderator: Nov 21, 2010
  2. crazyNut

    crazyNut New Member

    Joined:
    Nov 21, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0

    Have you googled it

    You can get a lot ways of doing that on google.

    One of them is
    a = a + b;
    b = a - b;
    a = a - b;
     
  3. seomanju

    seomanju Banned

    Joined:
    Nov 19, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for providing this code.
     
  4. kemnet

    kemnet New Member

    Joined:
    Nov 15, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    lol was helpful. but im having trouble trying to imagine a situation where i wont be able to use a temp var. and why do u prefer scans an print to cout<< and cin
     
  5. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    it's a tricky question in interviews for a job as a programmer.
     
  6. cindrilla

    cindrilla New Member

    Joined:
    Sep 26, 2010
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    hyderabad
    hey wht r u saying kemnet i dint get u
     
  7. kemnet

    kemnet New Member

    Joined:
    Nov 15, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    no nothing. i was just trying to imagine a situation where id love this code since i wont be able to use a Temporary Variable. but that got cleared up thanks
     
  8. cindrilla

    cindrilla New Member

    Joined:
    Sep 26, 2010
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    hyderabad
    hey u !
    i hadnt googled it i have just learnt this trick ya i agree tht there are several methods to do it i have just mentioned one of them okk
     
  9. cindrilla

    cindrilla New Member

    Joined:
    Sep 26, 2010
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    hyderabad
    hi frinds this can also be done in differnt methods
    i would lyk to provide an alternate method instead of previous one as



    a=a*b;
    b=a/b;
    c=a/b;
    okk
    hope this will be helpful:p
     
  10. kemnet

    kemnet New Member

    Joined:
    Nov 15, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    no nothing. i was just trying to imagine a situation where id love this code since i wont be able to use a Temporary Variable. but that got cleared up thanks
     
  11. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if b=0 what will happen?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice