a = a + b
b = a - b
a = a - b
also read: Swapping using pointer
also read: Swapping using third variable
Program:
#include <stdio.h>
#include <conio.h>
/* Swap numbers without using temporary variable By SlashMyCode.com*/
int main()
{
int a,b;
printf("Enter value for A= ");
scanf("%d",&a);
printf("Enter value for B= ");
scanf("%d",&b);
// Code to swap 'a' and 'b'
a = a + b;
b = a - b; // b becomes a
a = a - b; // a becomes b
printf("\n After Swapping: \n\n A= %d, B= %d \n", a, b);
/* Swap numbers without using temporary variable By SlashMyCode.com*/
getch();
return 0;
}
Output:
Flowchart:
Algorithm:
- Start
- Declare variable a and b.
- Accept values for a and b.
- a = a + b;
- b = a - b;
- a = a - b;
- Print values of a and b.
- Stop
No comments:
Post a Comment
Feel free to ask us any question regarding this post