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
Can we use visual studio for c?
Which of the following operators is incorrect and why? ( >=, <=, <>, ==)
How to write a code for reverse of string without using string functions?
Explain what is the most efficient way to store flag values?
What are local static variables? How can you use them?
explain how do you use macro?
Is c is a high level language?
What is the use of parallelize in spark?
printf(), scanf() these are a) library functions b) userdefined functions c) system functions d) they are not functions
Explain bitwise shift operators?
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
Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]
What do you mean by recursion in c?
can we change the default calling convention in c if yes than how.........?
Explain goto?