Swapping of given variable without using temporary variable

 Program:

#include<stdio.h>
#include<conio.h>
void main()
{
    int a, b;
    clrscr();
    printf("Enter any Two Numbers : ");
    scanf("%d%d", &a, &b);
    printf("\nBefore Swapping :");
    printf("\na=%d", a);
    printf("\nb=%d\n\n", b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("After Swapping :");
    printf("\na=%d", a);
    printf("\nb=%d", b);
    getch();
}

Output:

Enter any Two Numbers : 5    10
Before Swapping :
a=5
b=10
After Swapping 
a=10
b=5


No comments:

Post a Comment