Through this program two variables will exchange values between themselves. Swapping is a very popular problem in programming languages. Let's learn program swapping through this article.
First we take two variables n1 and n2. And the values of this variable are taken from the user. Then we take another third variable which is temporary variable. We have swapped two numbers using this variable.
C Program to Swap Two Numbers
#include<stdio.h>
int main(){
int n1, n2;
printf("Enter n1 and n2 \n");
scanf("%d%d",&n1,&n2);
printf("Before Swapping N1=%d and n2=%d\n",n1,n2);
int temp;
temp=n1;
n1=n2;
n2=temp;
printf("after Swapping N1=%d and n2=%d\n",n1,n2);
return 0;
}
Output
Enter n1 and n2
4
9
Before Swapping N1=4 and n2=9
after Swapping N1=9 and n2=4
First we take two variables n1 and n2. And the values of this variable are taken from the user. Then we take another third variable which is temporary variable. We have swapped two numbers using this variable.
Tags:
C Program