Right Click to Search

Sunday, July 4, 2024

Swap / Interchange two variables [numbers] without using Third Variable




Generally Swaping two number requires three variables , Let's Take look at Procedure of swaping two Number
Let -
int num1=20,num2=40,temp;
For Swaping Two numbers following procedure is used -
temp = num1;
num1 = num2;
num2 = temp;
Here is Program for : [Swap / Interchange two variables [numbers] without using Third Variable]
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
printf("\nEnter First Number : ");
scanf("%d",&num1);
printf("\nEnter Second Number : ");
scanf("%d",&num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
printf("\n Numbers after Exchange : num1 = %d and num2 = %d",num1,num2);
getch();
}
Output :
Enter First Number : 20
Enter Second Number : 40
Numbers after Exchange : num1 = 40 and num2 = 20

Tags / Keywords : |

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates