Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new

 Categories >> Software >> Programming Languages >> C

 C Interview Questions (2266) C++ Interview Questions (1121) VC++ Interview Questions (342) Delphi Interview Questions (510) Programming Languages AllOther Interview Questions (652)
Question
`write a program to swap Two numbers without using temp variable.`
Question Submitted By :: C
I also faced this Question!!     Answer Posted By

# 1
a=a+b;
b=a-b;
a=a-b;

 Is This Answer Correct ? 1381 Yes 156 No
Chandan

# 2
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n Enter the 2 numbers");
scanf("%d%d",&a,&b);
//swaping of 2 numbers without using temp variable
a=a+b;
b=a-b;
a=a-b;
/* or
a=a*b;
b=a/b;
a=a/b;
*/
printf("\n A = %d \n B = %d\n");
getch();
}

 Is This Answer Correct ? 588 Yes 119 No
Harisharumalla

# 3
void swap(int *a,int *b)
{
if(*a == *b)
return;
*a^=*b;
*b^=*a;
*a^=*b;
}

 Is This Answer Correct ? 458 Yes 195 No
Abhi

# 4
Swapping two variables in a single line with no temporary
variable..

b=(a+b)-(a=b);

so.. simple..

Prasanna. (prasanna7287@yahoo.co.in)

 Is This Answer Correct ? 347 Yes 137 No
Prasanna

# 5
# include "stdio.h"
main()
{
int a,b;
printf("enter two numbers for swaping");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("a is %d",a);
printf("b is %d",b);
} output:- takea,b value is 2,3 and give answers is3,2

 Is This Answer Correct ? 239 Yes 56 No
Guest

# 6
b=a+b
a=b-a
b=b-a

if a=3 and b=5
then now b=a+b=8
and a=8-a=8-3=5,now a=5
b=8-a=8-5=3,now b=3

so, a=5 and b=3

 Is This Answer Correct ? 197 Yes 58 No
Anantha

# 7
a=a+b;
b=a-b;
a=a-b;
printf("a %dand b %dis :",a,b);

 Is This Answer Correct ? 181 Yes 42 No
Nagakishorebabu

# 8
void main()
{
int a,b;
printf("enter the two numbers");
scanf("%d%d",&a,&b);
a^=b^=a^=b;
printf("%d%d",a,b);//swapped no
}

 Is This Answer Correct ? 118 Yes 66 No
Ravi Saini

# 9
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a &b);
a=a+b;
b=a-b;
a=a-b;
printf("The swapped values are:");
printf("a:%d",a);
printf("b:%d",b);
getch();
}

 Is This Answer Correct ? 73 Yes 23 No
Sree

# 10
main()
{
int a=2,b=3;
a=a+b;
b=a-b;
a=a-b;
printf("%d",&a);
printf("%d",&b);
getch();
}

 Is This Answer Correct ? 72 Yes 26 No
Sweety