Swapping of two numbers


Prev Tutorial Next Tutorial

C Program to Swap two numbers

Swap two numbers means exchange the values of two variables with each other. For example variable num1 contains 20 and num2 contains 40 after swap there values num1 contains 40 and num2 contains 20.

Example: Suppose we have three glass A, B, Temp. Glass A contain 30, Glass B contain 50 and Glass Temp is empty now we want to interchange these two glass (A, B) valuse. So we follow below steps...

  • First we take Glass A and there values place inside Glass Temp
  • Now Glass A is empty, take Glass B and there values place inside Glass A.
  • Now Glass B is empty, take Glass Tempa and there values place inside Glass B
  • Finall Both Glass values are exchange, Glass A contain 50 and Glass B contain 30.
swap two numbers

C Program to Swap two numbers using third variables

Example

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,c;
clrscr();
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("After swapping a=: %d b=: %d",a,b);
getch();
}

Output

Enter value of a : 10
Enter value of b : 20
After swapping : a=20 and b=10

C Program to Swap two numbers without using third variable

Example

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b;
clrscr();
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping \na=: %d\nb=: %d",a,b);
getch();
}

Output

Enter value of a : 10
Enter value of b : 20
After swapping : a=20 and b=10

C Program to Swap two numbers using Pointers

Example

#include<stdio.h>
#include<conio.h>

int main()
{
  int x,y,*b,*a,temp;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  printf("Before swapping : x= %d and y=%d\n",x,y);
  a = &x;
  b = &y;
  temp = *a;
  *a = *b;
  *b = temp;
  printf("After swapping : x= %d and y=%d\n",x,y);
  getch();
}

Output

Enter any two number : 10 20
Before Swapping : x=10 and y=20
After swapping : x=20 and y=10

C Program to Swap two numbers using call by Reference

Example

#include<stdio.h>
#include<conio.h>

int main()
{
  int x,y,*b,*a,temp;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  swap(&x, &y);
  printf("Before swapping : x= %d and y=%d\n",x,y);
 }
 void swap(int *a, int *y)
 {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
  printf("After swapping : x= %d and y=%d\n",x,y);
  getch();
}

Output

Enter any two number : 30 20
Before Swapping : x=30 and y=20
After swapping : x=20 and y=30

C Program to Swap two numbers using Bitwise XOR

Example

#include<stdio.h>
#include<conio.h>

int main()
{
  int x, y;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  printf("Before swapping : x= %d and y=%d\n",x,y);
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
  printf("After swapping : x= %d and y=%d\n",x,y);
  getch();
}

Output

Enter any two number : 4 5
Before Swapping : x=4 and y=5
After swapping : x=5 and y=4

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements