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 function to reverse char array ... without using second array
atoi, which takes a string and converts it to an integer. write a program that reads lines(using getline), converts each line to an integer using atoi and computes the average of all the numbers read. also compute the standard deviation
Why pointers are used?
What are the application of void data type in c?
print the pattern 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 if n=5
what is compiler
Describe newline escape sequence with a sample program?
What is the maximum length of an identifier?
If the size of int data type is two bytes, what is the range of signed int data type?
Write a program or provide a pseudo code to flip the 2nd bit of the 32 bit number ! (Phone Screen)
What is type qualifiers?
What is the difference between arrays and pointers?