SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE

Posted By: Matpal Media - 2:05 AM
#include<stdio.h>

int main(){

    int a=5,b=10;

    //Method one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

    //Method 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);

    // Method four
    a=5;b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);

    // Method five
    a=5,b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);

    return 0;
}

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.