C program to Swap 2 Numbers Without Using third variable

Leave a Comment
/* The Following program is used to swap 2 values without the need of 3rd variable. The C code  below utilizes the addition & subtraction to perform this operation */
    #include<stdio.h>
    #include<conio.h>  
    int main()
    {
    float a,b;
    printf("Enter 2 Numbers to be swaped \n");
    scanf("%f%f",&a,&b);
    printf("Before Swapping a=%f b=%f\n",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("After swapping a=%f b=%f\n",a,b);
    getch();
    return 0;

    }
--------------------------------------------------------------------------------------------------------------------

/* The Following program is used to swap 2 values without the need of 3rd variable. The C code  below utilizes the multiplication & division to perform this operation */
    #include<stdio.h>  
    #include<conio.h>  
    int main()  
    {
    float a,b;
    printf("Enter 2 Numbers to be swaped \n");
    scanf("%f%f",&a,&b);
    printf("Before Swapping a=%f b=%f\n",a,b);
    a=a*b;
    b=a/b;
    a=a/b;
    printf("After swapping a=%f b=%f\n",a,b);
    getch();
    return 0;

    }


Screenshot:

C program to Swap 2 Numbers Without Using third variable

0 comments: