Programming Example
Program to show swap of two no's without using third variable
In this program we will show how to swap of two no's without using the third variable.
// Program to show swap of two no's without using third variable
// Author Atnyla Developer
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter value for a & b: ");
scanf("%d%d",&a,&b);
a = a+b;
b = a-b;
a = a-b;
printf("after swapping the value of a & b: %d %d \n",a,b);
}
<b>Output 1 </b>
enter value for a & b: 2 3
after swapping the value of a & b: 3 2
Press any key to continue . . .
<b>Output 2 </b>
enter value for a & b: 12
56
after swapping the value of a & b: 56 12
Press any key to continue . . .
a = a+b; b = a-b; a = a-b; <> First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.