Search C Program | nimishsoft@gmail.com

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

11 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
  3. #include
    #include

    void main()
    {
    int x = 10, y = 15;
    x = x + y - (y = x);
    printf("x = %d and y = %d",x,y);
    getch();
    }

    you can refer to this link for detail answer about the topic:

    https://www.studytonight.com/c/programs/basic/swapping-two-numbers-program

    ReplyDelete
  4. https://www.studytonight.com/c/programs/basic/swapping-two-numbers-program

    ReplyDelete