C program to swap two numbers in C programming language means exchanging the values of two variables. In this swap c programming example, you will learn to swap two numbers using two different techniques.
1.Swapping Two Numbers with Third Variable
2. Swapping Two Numbers Without Using Third Variable
#include<stdio.h> #include<conio.h> void main() { int num1,num2,temp; clrscr(); printf("\n Enter First No:"); scanf("%d",&num1); printf("\n Enter Second No:"); scanf("%d",&num2); temp = num1; num1 = num2; num2 = temp; printf("\n After Swapping X value=%d",num1); printf("\n After Swapping Y value=%d",num2); getch(); }
#include<stdio.h> #include<conio.h> void main() { int x,y,temp; clrscr(); printf("\n Enter First No X=:"); scanf("%d",&x); printf("\n Enter Second No Y=:"); scanf("%d",&y); x=x+y; y=x-y; x=x-y; printf("\n After Swapping X value=%d",x); printf("\n After Swapping Y value=%d",y); getch(); }