How to write program in C for swapping two numbers without using third variable?

Ricky
Ricky
Member
2 Points
1 Posts

Hi,

I am beginner in C and I'm stuck in a program to swap two numbers without using third variable.

I know the program of swap two no's using temporary variable.

Please, help me.

Views: 9635
Total Answered: 2
Total Marked As Answer: 0
Posted On: 26-Jun-2016 01:28

Share:   fb twitter linkedin
Answers
Kailash Singh
Kailash...
Participant
206 Points
23 Posts
         

Hello Ricky 

Check this solution. 

x=10; y=20; 
 
x=x+y; -> x= 10+20=30
y=x-y;  -> y= 30-20 =10
x=x-y;  -> x=30-10=20;
Posted On: 29-Jun-2016 08:54
Smith
Smith
None
2568 Points
74 Posts
         

Use following program in c:

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

void main()
{
       int a=3,b=4;
       clrscr();
       printf("\n Before swapping :");
       printf("\n a = %d  and  b = %d",a,b);
       a=a+b;
       b=a-b;
       a=a-b;
       printf("\n a = %d and b = %d",a,b);
       getch();
}
Posted On: 06-May-2017 02:00
 Log In to Chat