write a program to swap Two numbers without using temp variable.

Answers were Sorted based on User's Feedback



write a program to swap Two numbers without using temp variable...

Answer / chandan

a=a+b;
b=a-b;
a=a-b;

Is This Answer Correct ?    1400 Yes 158 No

write a program to swap Two numbers without using temp variable...

Answer / harisharumalla

#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 ?    604 Yes 119 No

write a program to swap Two numbers without using temp variable...

Answer / abhi

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

Is This Answer Correct ?    461 Yes 195 No

write a program to swap Two numbers without using temp variable...

Answer / prasanna

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 ?    351 Yes 140 No

write a program to swap Two numbers without using temp variable...

Answer / guest

# 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 ?    243 Yes 58 No

write a program to swap Two numbers without using temp variable...

Answer / anantha

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 ?    198 Yes 59 No

write a program to swap Two numbers without using temp variable...

Answer / nagakishorebabu

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

Is This Answer Correct ?    181 Yes 43 No

write a program to swap Two numbers without using temp variable...

Answer / ravi saini

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 ?    119 Yes 66 No

write a program to swap Two numbers without using temp variable...

Answer / sree

#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 24 No

write a program to swap Two numbers without using temp variable...

Answer / sweety

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 27 No

Post New Answer

More C Interview Questions

What is function pointer and where we will use it

2 Answers   Infosys, NetApp,


What is an array in c?

0 Answers  


why java is called as a purely oops language.

3 Answers   TVS,


State the difference between realloc and free.

0 Answers   Aricent,


What is an object?

5 Answers  






Are the expressions * ptr ++ and ++ * ptr same?

0 Answers  


a=10;b= 5;c=3;d=3; if(a printf(%d %d %d %d a,b,c,d) else printf("%d %d %d %d a,b,c,d);

0 Answers   Wilco,


find the sum of two matrices and WAP for it.

0 Answers   Huawei,


Which type of language is c?

0 Answers  


whitch value return void main?

11 Answers  


1 1 2 1 2 3 1 2 3 4 1 2 3 1 2 1 generate this output using for loop

2 Answers  


write a program that accepts 3 numbers from the user. dispaly the values in a descending order.

3 Answers  


Categories