How to swap two numbers in C?

In this section you will learn how to swap two numbers in c without using third variable. We will use two variable for swapping the numbers. In this program we are assigning the default value to variable.

Swapp Two Numbers Program

Swapping mean interchange of the values.

Let’s suppose a user will input two number as a ‘10’ and ‘20’ then we will perform swapping operation on it. We will use only two variables for swapping the numbers.

How program will work?

Program of swapping two numbers in c without using third variable.


	#include<stdio.h>
	#include<conio.h>
		
	//SWAP OF TWO NUMBER PROGRAM USING TWO VARIABLES
	void main()
	{
		
		int num = 10, num1 = 20;
		clrscr();
		
		printf("BEFORE SWAPING OF TWO NUMBER :- %d %d",num,num1);
		num = num + num1; //30
		num1 = num - num1; //10
		num = num - num1;

		printf("AFTER SWAPING OF TWO NUMBER :- %d %d",num,num1);

		getch();
	}
	

Output:


	BEFORE SWAPING OF TWO NUMBER :- 10 20
	AFTER SWAPING OF TWO NUMBER :- 20 10

Post Your Comment