➤ Swap two numbers using third variable .
#include<stdio.h>
int main()
{int a,b,temp;
printf("Enter a= ");
scanf ("%d",&a);
printf("Enter b= ");
scanf("%d",&b);
printf("Before Swapping:\n");
printf("a=%d\nb=%d",a,b);
temp=a;
a=b;
b=temp;
printf("\nAfter Swapping:");
printf("\na=%d\nb=%d",a,b);
return 0;
}
➤ OUTPUT :
Enter a= 5
Enter b= 9
Before Swapping:
a=5
b=9
After Swapping:
a=9
b=5
➤ Swap two numbers without using third variable .
#include<stdio.h>
int main()
{int a,b;
printf("Enter the value of a= ");
scanf ("%d",&a);
printf("Enter the value of b= ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter Swapping:");
printf("\na=%d\nb=%d",a,b);
int main()
{int a,b;
printf("Enter the value of a= ");
scanf ("%d",&a);
printf("Enter the value of b= ");
scanf("%d",&b);
printf("Before Swapping:\n");
printf("a=%d\nb=%d",a,b);a=a+b;
b=a-b;
a=a-b;
printf("\nAfter Swapping:");
printf("\na=%d\nb=%d",a,b);
return 0;
}
}
➤ OUTPUT :
Enter value of a= 5
Enter value of b= 3
Before Swapping:
a=5
b=3
After Swapping:
a=3
b=5