How many ways are there to swap two numbers without using
temporary variable? Give the each logic.

Answers were Sorted based on User's Feedback



How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / vignesh1988i

as for i know their are two different logics for the
swapping techinque.... swapping is a type of techinque used
for interchanging the two varibles in the operating
memory....
1) using only variables
2) using EX-OR operator '^'
first logic only know.
void main()
{
int a,b;
printf("enter the a&b");
scanf("%d%d",&a,&b);
a+=b;
b=a-b;
a-=b;
printf("a=%d\nb=%d",a,b);
getch();
}

Is This Answer Correct ?    48 Yes 4 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / venkatesh sabinkar

as far i know i know two logics they are
first one
void main()
{
int a,b;
printf("eneter any two values");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d\nb=%d",a,b);
getch():
}
second one
void main()
{
int a,b;
printf("eneter any two values");
scanf("%d%d",&a,&b);
a=a*b;
b=a/b;
a=a/b;
printf("a=%d\nb=%d",a,b);
getch():
}

Is This Answer Correct ?    24 Yes 1 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / kishan gowda

One more method its bit complicated from the people from
the non electronics background......
void main()
{
pf(" a and b ");
sf("%d %d", &a, &b);
a= a^b;
b= b^a;
a= a^b;
pf("Swapped no's are a=%d and b=%d" ,a ,b);
}

Is This Answer Correct ?    24 Yes 4 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / alok bajpai

You can swap two Numbers without using temporary variable
with the help of EX-OR operator;

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the first No.:");
scanf("%d",&a);
printf("Enter the second No.:");
scanf("%d",&b);
a^=b^=a^=b;
printf("%d %d",a,b);
getch();
}

Is This Answer Correct ?    19 Yes 2 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / gana samantula

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(" Enter the first No.:");
scanf("%d",&a);
printf(" Enter the second No.:");
scanf("%d",&b);
b=a+b-(a=b);
printf(" the swap of a and b numbers :%d %d",a,b);

getch();
}

Is This Answer Correct ?    14 Yes 0 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / kk

three logic
xor -> a^=b^=a^=b;
addition and subtraction,
multiply and division .......

Is This Answer Correct ?    8 Yes 3 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / chandan doki

void main()
{
int a,b;
printf("eneter any two values for a and b");
scanf("%d%d",&a,&b);
b=a+b;
a=b-a;
b=b-a;
printf("a=%d\nb=%d",a,b);
getch();
}

Is This Answer Correct ?    2 Yes 0 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / tumatij

/*5. Write a C program to swap two integer numbers in various different techniques.
Answer:
*/
#include<stdio.h>
int main()
{
int a,b,temp;
printf("Enter the two numbers:");
scanf("%d%d",&a,&b);
printf("
Values before swapping a=%d b=%d",a,b);
//First logic
temp=a;
a=b;
b=temp;
printf("
Values after swapping a=%d b=%d",a,b);
//2nd logic
a=a+b;
b=a-b;
a=a-b;
printf("
Values after swapping a=%d b=%d",a,b);
//3rd logic
a=a*b;
b=a/b;
a=a/b;
printf("
Values after swapping a=%d b=%d",a,b);
//4th logic
a=(a+b)-(b=a);
printf("
Values after swapping a=%d b=%d",a,b);
//5th logic
a=a^b;
b=a^b;
a=a^b;
printf("
Values after swapping a=%d b=%d",a,b);
//6th logic
a^=b^=a^=b;
printf("
Values after swapping a=%d b=%d",a,b);
//7th logic
a=a-b;
b=a+b;
a=b-a;
printf("
Values after swapping a=%d b=%d",a,b);
}

Is This Answer Correct ?    0 Yes 0 No

How many ways are there to swap two numbers without using temporary variable? Give the each logic...

Answer / megha

in 7 types

Is This Answer Correct ?    2 Yes 11 No

Post New Answer

More C Interview Questions

Compare and contrast compilers from interpreters.

0 Answers  


how to find out the inorder successor of a node in a tree??

2 Answers   TCS, Yahoo,


c programs are converted into machine language with the help of a) an interpreter b) a compiler c) an operatinf system d) none of the above

0 Answers  


hi any body pls give me company name interview conduct "c" language only

0 Answers  


CAN WE DEFINE ANY FUNCTION WITHIN A FUNCTION.

15 Answers  






write a c program to print a given number as odd or even without using loop statements,(no if ,while etc)

10 Answers  


how to create duplicate link list using C???

0 Answers  


An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above

0 Answers  


1. Can we use the for loop this way? for(;i>5;) 2. What is the use of pointers? 3. what is array of pointer? 4. What is the difference between file and database? 5. Define data structure?

1 Answers  


Write one statement equalent to the following two statements x=sqr(a); return(x); Choose from one of the alternatives a.return(sqr(a)); b.printf("sqr(a)"); c.return(a*a*a); d.printf("%d",sqr(a));

6 Answers   TCS,


How #define works?

0 Answers  


write a program to generate address labels using structures?

0 Answers   SJC,


Categories