Search C Program

Loading...

Swap two values without using third variable

/*    Swap the values of two variables without using third variable   */

#include<stdio.h>

int main()
{
 int a;
 int b;
 printf("Type value of A : ");
 scanf("%i",&a);
 printf("\nType value of b : ");
 scanf("%i",&b);
  a = a + b;
  b = a - b;
  a = a - b ;

 printf("A : %i",a);
 printf("\nB : %i",b);
 return 0;
}



Related Posts:
------------------------------------------------------------------------------
Swap values of two variables using XOR
http://cbasicprogram.blogspot.com/2012/01/swap-two-variables-using-xor.html

Swap values of two variables
http://cbasicprogram.blogspot.com/2012/01/swap-values-of-two-variables-c-program.html

8 comments:

  1. how to swap values of 2 variables using function where arguments are passed by reference

    ReplyDelete
  2. hi all

    even this works

    a = a ^ b;
    b = a ^ b;
    a = a ^ b ;


    and


    a = a * b;
    b = a / b;
    a = a / b ;

    ReplyDelete