Swap Two Numbers Program in C

Google Advertisements

Prev Tutorial Next Tutorial

Swapping of Two Numbers Program in C

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

Swap Two Numbers Program in C

#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

Swap Two Numbers Program in C

#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

Swap 2 Numbers Program in C

#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

Swap Two Numbers Program in C

#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

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483