Introduction:
This is very common question asked by Interviewer to fresher to check their basics.
In this article I will explain swapping using
a temporary variable or third variable.
Program example: Let's understand by an program
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,temp;
clrscr();
printf("Enter
the value of a= ");
scanf("%d",&a);
printf("\nEnter
the value of b= ");
scanf("%d",&b);
printf("\nValue
of a before swapping= %d",a);
printf("\nValue
of b before swapping= %d",b);
temp=a;
a=b;
b=temp;
printf("\nValue
of a after swapping= %d",a);
printf("\nValue
of b after swapping= %d",b);
getch();
}
Run the
program using ctrl+F9
Output:
Enter the
value of a=11
Enter the
value of b=22
Value of a
before swapping=11
Value of b
before swapping=22
Value of a
after swapping=22
Value of b
after swapping=11
0 comments:
Post a Comment