Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables

Table of Contents
< All Topics
Print

Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables

Aim

To write a C program to swap two number a) using temporary variable and b) without using temporary variable and c) without using temporary variable and use bitwise operator.

In these three ways, one can easily write a program to swap two numbers using temporary variables and without using temporary variables.

Procedure

Step-1: Start the program execution

Step-2: Declare num1, num2, temp.

Step-3: Read 2 values num1 and num2.

Step-4: A) using temporary variable: temp=num1, num1=num2, num2=temp;

Print the values of num1 and num2.

Step-5: B)without using temporary variable: num1= num1+num2, num2= num1-num2, num2=num1-num2.

Print the values of num1 and num2.

Step-6: C) without using temporary variable but apply XoR operator:

num1= num1+num2, num2= num1^num2, num2^num1^num2.

Print the values of num1 and num2.

Step-6: Stop the program execution.

Coding

A. By using temporary variable.

// using third variable as temp.
// author milind bhatt
#include<stdio.h>

int main()
{
int num1, num2, temp;

printf("\nEnter two values:\n");
scanf("%d%d",&num1,&num2);
printf("\nSwapping with temporary variables:\n\n");
printf("\nBefore swapping the values are num1 = %d and num2 =%d",num1,num2);

temp=num1;
num1=num2;
num2=temp;

printf("\nAfter swapping the values are num1=%d and num2=%d",num1,num2);

return 0;
}// end main

Output


Enter two values:
12
14

Swapping with temporary variables:


Before swapping the values are num1 = 12 and num2 =14
After swapping the values are num1=14 and num2=12
--------------------------------

B. Without using temporary variable.

// without using third variable as temp.
// author milind bhatt
#include<stdio.h>

int main()
{
int num1, num2, temp;

printf("\nEnter two values:\n");
scanf("%d%d",&num1,&num2);
printf("\nSwapping with temporary variables:\n\n");
printf("\nBefore swapping the values are num1 = %d and num2 =%d",num1,num2);


num1=num1+num2;
num2=num1-num2;
num1=num1-num2;

printf("\nAfter swapping the values are num1=%d and num2=%d",num1,num2);

return 0;
}// end main
Enter two values:
12
68

Swapping with temporary variables:


Before swapping the values are num1 = 12 and num2 =68
After swapping the values are num1=68 and num2=12
--------------------------------

C. By using bitwise XOR operator.

// without using third variable and using btwise operator
// author milind bhatt
#include<stdio.h>

int main()
{
int num1, num2, temp;

printf("\nEnter two values:\n");
scanf("%d%d",&num1,&num2);
printf("\nSwapping with temporary variables:\n\n");
printf("\nBefore swapping the values are num1 = %d and num2 =%d",num1,num2);


//swapping two numbers without third variable and usng bitwise operator
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;

printf("\nAfter swapping the values are num1=%d and num2=%d",num1,num2);

return 0;
}// end main

Output


Enter two values:
10
20

Swapping with temporary variables:


Before swapping the values are num1 = 10 and num2 =20
After swapping the values are num1=20 and num2=10
--------------------------------

Run Directly on in-built C-Compiler

RESULT

Thus, the C program has been written to swap two variables using temporary variable and without using temporary variable. These are the three major ways to swap to numbers. Most of the times one can use any single method but in case of smart coding I will suggest using a programming approach of without third variable.


Recommended Readings

Leave a comment