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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we use visual studio for c?

780


Which of the following operators is incorrect and why? ( >=, <=, <>, ==)

908


How to write a code for reverse of string without using string functions?

1841


Explain what is the most efficient way to store flag values?

941


What are local static variables? How can you use them?

856


explain how do you use macro?

912


Is c is a high level language?

864


What is the use of parallelize in spark?

766


printf(), scanf() these are a) library functions b) userdefined functions c) system functions d) they are not functions

872


Explain bitwise shift operators?

927


i have to apply for rbi before that i need to know the the syllabus for the entrance questions. whethet it may be aps or techinical

2093


Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]

840


What do you mean by recursion in c?

863


can we change the default calling convention in c if yes than how.........?

2295


Explain goto?

924