1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

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

    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


    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

    Thanks for providing this code.
     
  4. kemnet

    kemnet New Member

    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 New Member

    it's a tricky question in interviews for a job as a programmer.
     
  6. cindrilla

    cindrilla New Member

    hey wht r u saying kemnet i dint get u
     
  7. kemnet

    kemnet New Member

    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

    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

    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

    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 New Member

    if b=0 what will happen?
     

Share This Page