SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE

SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE


#include<stdio.h>
int main(){
    int a,b;
    printf("\nEnter two numbers:");
    scanf("%d %d",&a,&b);
    printf("\nBefore swapping a=%d b=%d",a,b);
    a=a^b;
    b=b^a;
    a=a^b;
    printf("\nAfter swapping a=%d b=%d",a,b);
    return 0;
}

OR
Swapping of two number
#include<stdio.h>
int main(){
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
 
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
 
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    getch();

}


More C Program :


  1. SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
  2. c program to swap two numbers without using third variable
  3. c program to swap two numbers without using third variable using functions
  4. program to swap two numbers without using third variable in c language
  5. program to swap two numbers without using third variable in php
  6. program to swap two numbers without using third variable in java
  7. c program to swap two numbers without using temporary variable
  8. write a program to swap two variables without using a temporary variable
  9. c program to swap two numbers using third variable


Learn More :