Write a program to swap two no without using a third variable.

swap without variable
#include<conio.h>
#include<stdio.h>
main()
{
	int a=4,b=6;
	a=a+b;
	b=a-b;
	a=a-b;
	printf("The no a=%d & b=%d",a,b);
	getch();
}

/* Explanation:-
In the above program, first two variables a & b are declared and initialized.
values of a & b are added in the statement a=a+b.In the next statement b is subtracted from a and
result is stored in b.Finally,the value of a is obtained by subtracting b from a.
The printf() statement prints the value.*/ 

The above code is a C program that swaps the values of two variables a and b without using a temporary variable.

The program starts by including the stdio.h and non-standard conio.h header file. Then it defines the main() function, which is the starting point of the program. Inside the main function, it declares two integer variables a and b and assigns the values 4 and 6 respectively.

The values of a and b are then swapped using mathematical operations like addition and subtraction. The value of a is first added to the value of b, then the value of b is assigned the value of a subtracting b from the sum of a and b, Finally the value of a is assigned the value of a subtracting b from the sum of a and b.

After the swapping, the program uses the printf() function to display the new values of a and b on the screen. The program uses the getch() function which waits for the user to press a key before terminating the program.

The program also includes comments explaining the code and how the swapping is being done. Comments are a good practice in programming as they make the code more readable and easy to understand for other developers who may need to work with it in the future. It’s important to note that the getch() function is not a part of the standard C library, it is provided by the non-standard conio.h header file and it’s use is not recommended in most cases.

This can be useful in a wide range of applications, such as:

  1. Sorting algorithms: Many sorting algorithms, such as bubble sort and selection sort, involve repeatedly swapping the values of adjacent elements in an array.
  2. Data manipulation: Swapping values can be used to rearrange data in a specific order or to change the order of elements in an array or list.
  3. Bit manipulation: In certain cases, swapping two variables can be a more efficient way to perform bit manipulation operations, such as swapping the values of two bits in a binary number.
  4. Game development: In game development, swapping values can be used to implement various game mechanics, such as swapping the positions of two game objects on a screen.
  5. Optimization: Swapping values can be used to optimize certain computations by reducing the number of operations required or by reducing the memory usage.
  6. Algorithm analysis: Swapping values can be used in the analysis of algorithms to determine their time and space complexity.

Overall, the swapping of variables is a common operation in many programming tasks, and having a program that can perform this operation quickly and efficiently can be very useful.

Thanks

Write a program to swap two no without using a third variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top