Swap two numbers in C

Swapping two numbers is a fundamental concept in programming, often serving as an introductory exercise for beginners. In C programming, there are multiple ways to swap two numbers. This article will delve into three different methods to Swap two numbers in C, each with a detailed explanation.

Learn how to “Swap two numbers in C”

Method 1) Swap two numbers in C using a Temporary Variable

This straightforward method commonly introduces the concept of swapping. It uses a temporary variable to hold one of the numbers during the swap process.

Steps:

  1. Store the value of the first variable in a temporary variable.
  2. Assign the value of the second variable to the first variable.
  3. Assign the value of the temporary variable to the second variable.

Code Example

#include <stdio.h>

int main() {
    int a, b, temp;

    // Initializing variables
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    // Swapping using a temporary variable
    temp = a;
    a = b;
    b = temp;

    // Output the result
    printf("After swapping: a = %d, b = %d\n", a, b);
    
    return 0;
}

Explanation of the Code

  1. The program declares three integer variables: a, b, and temp.
  2. It prompts the user to input two integers, which it stores in a and b.
  3. It stores the value of a in temp.
  4. Then, it assigns the value of b to a.
  5. Finally, it assigns the value stored in temp (originally a) to b.

Method 2) Swap two numbers in C using Arithmetic Operations

This method eliminates the need for a temporary variable by using arithmetic operations. It relies on the properties of addition and subtraction to swap the values.

Steps:

  1. Add the two numbers and store the result in the first variable.
  2. Subtract the second variable from the first variable and assign the result to the second variable.
  3. Subtract the updated second variable from the first variable and assign the result to the first variable.

Code Example

#include <stdio.h>

int main() {
    int a, b;

    // Initializing variables
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    // Swapping using arithmetic operations
    a = a + b;
    b = a - b;
    a = a - b;

    // Output the result
    printf("After swapping: a = %d, b = %d\n", a, b);
    
    return 0;
}

Explanation of the Code

  1. The program begins by declaring two integer variables, a and b.
  2. The user inputs two integers, which are stored in a and b.
  3. The values of a and b are added and the result is stored in a.
  4. The value of b is subtracted from the new value of a (which is the sum of a and b), and the result is stored in b.
  5. Finally, the updated value of b is subtracted from the new value of a (still the sum of the original a and b), and the result is stored in a.

Method 3) Swap two numbers in C using Bitwise XOR Operator in C

This method uses the XOR bitwise operator to swap the numbers without using a temporary variable. It is a bit more advanced but demonstrates the versatility of bitwise operations in C.

Steps:

  1. Apply the XOR operation between the two numbers and store the result in the first variable.
  2. Apply the XOR operation between the updated first variable and the second variable, and store the result in the second variable.
  3. Then, apply the XOR operation between the updated first variable and the updated second variable, and store the result in the first variable.

Code Example

#include <stdio.h>

int main() {
    int a, b;

    // Initializing variables
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    // Swapping using bitwise XOR operator
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;

    // Output the result
    printf("After swapping: a = %d, b = %d\n", a, b);
    
    return 0;
}

Explanation of the Code

  1. The program declares two integer variables, a and b.
  2. The user inputs two integers, which it stores in a and b.
  3. It performs the XOR operation between a and b and stores the result in a.
  4. hen, it performs the XOR operation between the updated a and b and stores the result in b.
  5. Finally, it performs the XOR operation between the updated a and the updated b and stores the result in a.
Categories C

Leave a Comment