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
There is a practice in coding to keep some code blocks in comment symbols than delete it when debugging. How this affect when debugging?
Linked list is a Linear or non linear explain if linear how it working as a non linear data structures
Can true be a variable name in c?
Why c is called top down?
difference between Low, Middle, High Level languages in c ?
write a program in C that prompts the user for today's date,tomorrow's date and display the results.Use structures for today's date,tomorrow's date and an array to hold the days for each month of the year.
what are # pragma staments?
How can I run c program?
Why we use conio h in c?
What are types of preprocessor in c?
Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create.
What was noalias and what ever happened to it?
Why is c called c not d or e?
What is a constant and types of constants in c?
What is a pragma?