write a program to swap Two numbers without using temp variable.
Answers were Sorted based on User's Feedback
Answer / vigneswari
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c,d;
cout<<"\n enter a:";
cin>>a;
cout<<"\n enter b:";
cin>>b;
c=a+b;
d=a-b;
c=a-b;
cout<<"\n answer a :"<<c;
cout<<"\n answer b:"<<d;
getch();
}
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / ashish omar
Suppose: a = 3
b=2;
11
10
a = a ^ b;
11
10
==
01
b = a ^ b;
01
10
==
11
a = a ^ b;
01
11
==
10
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / dhruv
I am not giving an answer, but just wanted to point out that
most of the solutions which try to add (a+b) and then
subtract from it will not work if (a+b) exceeds the maximum
value an int can hold.
So even if it looks good on paper it will give erroneous
results when run on a machine
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / bhokal
#include <stdio.h>
#include <stdlib.h>
void main()
{
int p,r;
printf("enter no to b swaped:\n");
scanf("%d%d",&p,&r);
printf("swapped no . are\n%d\n%d\n",r,p);
getch();
}
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / ashish kumar sharma
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the Value of a:\n");
scanf("%d",&a);
printf("enter the Value of b:\n");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("Display The Swapping:\n");
printf("a=%d And b=%d",a,b);
getch();
}
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / rakesh
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=8;
a=a+b;//a=10+8=18
b=a-b;//b=18-8=10
a=a-b;//a=18-10=8
//hence a=8,b=10
}
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / guest
using xor technique is much faster than using addition and
subtraction process , and xor instruction is much simpler at
processor level.
so be simply
x = x xor y
y = x xor y
x = x xor y
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / chandan
#!/usr/bin/perl
print "Enter Values for a & b\n";
my ($a,$b);
$a=<STDIN>;
$b=<STDIN>;
print "Value of A & B Befor Swap\n";
print "Value of a = $a\n";
print "Value of b = $b\n";
$b=($a+$b)-($a=$b);
print "Value of A & B After Swaping\n";
print "Value of a = $a\n";
print "Value of b = $b\n";
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / tamal datta
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n Enter a = ");
scanf("%d",&a);
printf("\n Enter b = ");
scanf ("%d",&b);
printf ("\nBefore swapping a = %d",a);
printf ("\nBefore swapping b = %d",b);
//swaping of 2 numbers without using temp variable
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping a = %d",a);
printf ("\nAfter swapping b= %d",b);
getch();
}
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / kabita shah
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
printf("enter two value");
scanf("%d%d",&x,&y);
x=x+y;
y=x-y;
x=x-y;
printf("value of x=%d",x);
printf("value of y=%d",y);
getch();
}
Is This Answer Correct ? | 1 Yes | 0 No |
write a c program to change only the 3rd bit of the particular number such that other bits are not affected.. if bitnum=10(say.. it can be any no..
What are keywords in c with examples?
How is a pointer variable declared?
Are local variables initialized to zero by default in c?
Is c procedural or functional?
What tq means in chat?
What is the heap?
Write a code to generate a series where the next element is the sum of last k terms.
Why isnt there a numbered, multi-level break statement to break out
What is variables in c?
Explain Doubly Linked Lists?
? ???Mirror Mirror on the wall????????