// Program to show swap of two no’s without using third variable
#include<stdio.h>
#include<conio.h>
void main() {
int a, b;
printf("\nEnter value for num1 & num2 : ");
scanf("%d %d", &a, &b);
a=a+b-(a=b); //Swaping
printf("\nAfter swapping value of a : %d", a);
printf("\nAfter swapping value of b : %d", b);
getch();
}
Bhavesh Nariya
0
Newbie Poster
Recommended Answers
Jump to PostIs there a question to this, or are you wanting to just post a code snippet? As I read it, your code to actually do the swap will not work. You will need to assign to both
a
andb
to perform a swap, but you just assign toa
…
Jump to PostThere are many ways to do it. You can use the XOR operations as ruberman has done it.
You can try one more method using addition and substraction:
void swap(int a,int b) { a = a + b; b = a - b; a = a - …
Jump to Post"Number" is ambiguous. Let's throw a wrench into the mix:
double a, b;
Beware, the answer isn't as simple as you think. :D
Jump to PostThe solution I gave will work for the int surely but it might give wrong for the double. Am I right?
You are correct, sir! And why is that? :)
All 13 Replies
Nutster
58
Newbie Poster
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
DonnSchwartz
0
Newbie Poster
nitin1
15
Master Poster
pawar8390
0
Newbie Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
nitin1
15
Master Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
nitin1
15
Master Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
Lardmeister
461
Posting Virtuoso
Nutster
commented:
You need to assign to b to actually swap.
-1
Oddytech
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.