In C, a Swap Two Numbers program exchanges the values of two variables, meaning that the value of the first variable is assigned to the second, and the value of the second is assigned to the first. This can be achieved either by using a temporary variable or without a temporary variable using arithmetic or bitwise operations.

Methods to Swap Two Numbers:

  1. Using a Temporary Variable.
  2. Without a Temporary Variable using arithmetic operations.
  3. Without a Temporary Variable using bitwise XOR operator.

Method 1: Swapping Two Numbers Using a Temporary Variable

This is the most straightforward method where we use a third variable to temporarily hold the value of one of the numbers during the swap.

Program:

#include <stdio.h> int main() { int a, b, temp; // Declare three variables // Input two numbers from the user printf("Enter two numbers: "); scanf("%d %d", &a, &b); // Display values before swapping printf("Before swapping: a = %d, b = %d\n", a, b); // Swap the values using a temporary variable temp = a; // Store the value of 'a' in 'temp' a = b; // Assign the value of 'b' to 'a' b = temp; // Assign the value stored in 'temp' to 'b' // Display values after swapping printf("After swapping: a = %d, b = %d\n", a, b); return 0; }

Explanation:

  1. Variables:

    • int a, b, temp: a and b are the two numbers to be swapped, and temp is the temporary variable used to store one value during the swap.
  2. Input:

    • scanf("%d %d", &a, &b); takes two integer inputs from the user and stores them in a and b.
  3. Swapping Logic:

    • temp = a; stores the value of a in the temporary variable temp.
    • a = b; assigns the value of b to a.
    • b = temp; assigns the value stored in temp (the original value of a) to b.
  4. Output:

    • The values of a and b are printed both before and after the swap to demonstrate the swapping.

Sample Output:

Example:

Enter two numbers: 5 10 Before swapping: a = 5, b = 10 After swapping: a = 10, b = 5

Method 2: Swapping Two Numbers Without a Temporary Variable (Using Arithmetic Operations)

We can also swap two numbers using arithmetic operations like addition and subtraction.

Program:

#include <stdio.h> int main() { int a, b; // Declare two variables // Input two numbers from the user printf("Enter two numbers: "); scanf("%d %d", &a, &b); // Display values before swapping printf("Before swapping: a = %d, b = %d\n", a, b); // Swap without a temporary variable using addition and subtraction a = a + b; // Add 'a' and 'b' and store the result in 'a' b = a - b; // Subtract the new value of 'a' from 'b' and store it in 'b' a = a - b; // Subtract the new value of 'b' from 'a' and store it in 'a' // Display values after swapping printf("After swapping: a = %d, b = %d\n", a, b); return 0; }

Explanation:

  1. Swapping Logic:

    • a = a + b;: This adds a and b and stores the result in a.
    • b = a - b;: The new value of a is subtracted from b, which gives the original value of a, and this is assigned to b.
    • a = a - b;: The new value of b (which is the original value of a) is subtracted from the new value of a, which gives the original value of b, and this is assigned to a.
  2. Output:

    • The program prints the values of a and b both before and after the swap, demonstrating the arithmetic-based swapping.

Sample Output:

Example:

Enter two numbers: 15 25 Before swapping: a = 15, b = 25 After swapping: a = 25, b = 15

Method 3: Swapping Two Numbers Without a Temporary Variable (Using XOR Bitwise Operator)

Another way to swap numbers without a temporary variable is by using the XOR (^) operator.

Program:

#include <stdio.h> int main() { int a, b; // Declare two variables // Input two numbers from the user printf("Enter two numbers: "); scanf("%d %d", &a, &b); // Display values before swapping printf("Before swapping: a = %d, b = %d\n", a, b); // Swap without a temporary variable using XOR a = a ^ b; // XOR the values of 'a' and 'b' and store in 'a' b = a ^ b; // XOR the new value of 'a' with 'b' to get the original value of 'a' a = a ^ b; // XOR the new value of 'a' with the new value of 'b' to get the original value of 'b' // Display values after swapping printf("After swapping: a = %d, b = %d\n", a, b); return 0; }

Explanation:

  1. Swapping Logic:

    • a = a ^ b;: XOR a and b, and store the result in a.
    • b = a ^ b;: XOR the new value of a (which contains a ^ b) with b. This results in the original value of a, which is then assigned to b.
    • a = a ^ b;: XOR the new value of a (which contains a ^ b) with the new value of b (which now contains the original value of a). This gives the original value of b, which is assigned to a.
  2. Output:

    • The program prints the values of a and b both before and after the swap, demonstrating the XOR-based swapping.

Sample Output:

Example:

Enter two numbers: 30 45 Before swapping: a = 30, b = 45 After swapping: a = 45, b = 30

Key Points:

  • Using a Temporary Variable: The most intuitive and common method.
  • Without a Temporary Variable (Arithmetic): Uses addition and subtraction but can lead to overflow for large values.
  • Without a Temporary Variable (XOR): Works using bitwise operations but might be harder to understand for beginners.

Each of these methods swaps two numbers, demonstrating different ways to perform the same task in C.