Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a program that swaps the values of two variables without using a temporary variable

Program in C

C
#include <stdio.h>

void swap(int *a, int *b) {
    *a = *a + *b;
    *b = *a - *b;
    *a = *a - *b;
}

int main() {
    int num1 = 10;
    int num2 = 20;

    printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

    // Swap the values of num1 and num2
    swap(&num1, &num2);

    printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}

Explanation:

  • In this program, the swap function takes two pointers to integers (a and b).
  • The swapping is achieved without using a temporary variable by utilizing the properties of addition and subtraction.
  • Here’s how it works:
  • Add *a and *b, and store the result in *a.
  • Now *a contains the sum of the original values of *a and *b.
  • Subtract the original value of *b from the updated *a, and store the result in *b.
  • Now *b contains the original value of *a.
  • Finally, subtract the original value of *b from the updated *a, and store the result in *a.
  • Now *a contains the original value of *b, effectively swapping the values.

In the main function, we demonstrate the usage of the swap function by swapping the values of two integers (num1 and num2).

Output:

C
Before swapping: num1 = 10, num2 = 20
After swapping: num1 = 20, num2 = 10

Can we create swapping programmes without using temporary variables or pointers in C?

Swapping the values of two variables without using a temporary variable or pointers is not possible in C. The use of a temporary variable or pointers is necessary to perform the swap operation. Pointers allow us to indirectly access and modify the values stored in variables. Without pointers, we cannot achieve the necessary indirection required to swap the values without a temporary variable.


Program in Java

Java
public class SwapNumbers {
    public static void swap(int[] arr) {
        arr[0] = arr[0] + arr[1];
        arr[1] = arr[0] - arr[1];
        arr[0] = arr[0] - arr[1];
    }

    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;

        System.out.println("Before swapping: num1 = " + num1 + ", num2 = " + num2);

        // Create an array to hold the values of num1 and num2
        int[] arr = {num1, num2};

        // Swap the values using the swap method
        swap(arr);

        // Retrieve the swapped values from the array
        num1 = arr[0];
        num2 = arr[1];

        System.out.println("After swapping: num1 = " + num1 + ", num2 = " + num2);
    }
}

Explanation:

  1. public class SwapNumbers: This line declares a public class named SwapNumbers.
  2. public static void swap(int[] arr): This is a static method named swap that takes an integer array arr as a parameter. The method performs the swap operation on the array elements.
  3. arr[0] = arr[0] + arr[1];, arr[1] = arr[0] – arr[1];, and arr[0] = arr[0] – arr[1];: These lines perform the swap operation by using arithmetic operations without using a temporary variable, similar to the original C code.
  4. public static void main(String[] args): This is the main method that serves as the entry point of the program.
  5. int num1 = 10; and int num2 = 20;: These lines initialize the variables num1 and num2 with the values 10 and 20, respectively.
  6. System.out.println(“Before swapping: num1 = ” + num1 + “, num2 = ” + num2);: Prints a message indicating the values before swapping.
  7. int[] arr = {num1, num2};: Creates an integer array arr and assigns it the values of num1 and num2. This is done to simulate passing the variables by reference as in the original C code.
  8. swap(arr);: Calls the swap method and passes the array arr as an argument to swap its elements.
  9. num1 = arr[0]; and num2 = arr[1];: Retrieves the swapped values of num1 and num2 from the array arr.
  10. System.out.println(“After swapping: num1 = ” + num1 + “, num2 = ” + num2);: Prints a message indicating the values after swapping.
Output
Before swapping: num1 = 10, num2 = 20
After swapping: num1 = 20, num2 = 10