C Program to Swap Two Numbers.

Swapping is a way of exchanging the values of two different variables. In C programming, we have multiple approaches to swap two numbers efficiently. In this article, we will learn each approach one by one.

Swap two numbers

Approach 1: Swap two numbers using a Temporary variable.

In this approach, we take the help of a third temp variable for swapping values.

 

Step-by-step algorithm:

Step 1: Input the two numbers from the user.

Step 2: Create a temporary variable to hold the value of one of the numbers.

Step 3: Assign the value of the first number to the temporary variable.

Step 4: Assign the value of the second number to the first number.

Step 5: Assign the value of the temporary variable to the second number.

Step 6: Print the swapped values of the two numbers.


Below is the C program to swap two numbers using the third temporary variable.

//C Program to swap two numbers usingn third variable
#include <stdio.h>

int main() {
    int num1, num2, temp;

    // Input the two numbers
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Create a temporary variable and swap the numbers
    temp = num1;
    num1 = num2;
    num2 = temp;

    // Print the swapped values
    printf("Swapped values: %d %d\n", num1, num2);

    return 0;
}
Output:
Enter two numbers: 20 10
Swapped values: 10 20

Time Complexity: As it is performing fix number of operations regardless of input so time complexity is O(1).
Space Complexity: As no extra space is required except for one variable so space complexity is O(1)


Approach 2: Swap two numbers using Arithmetic Operations.

In this approach, we do not require any third temp variable for swapping, instead, we perform some arithmetic operations to do so.

Step-by-step algorithm:

Step 1: Input the two numbers from the user.
Step 2: Perform the following arithmetic operations:
  • Add the first number to the second number and store the result in the first number.
  • Subtract the second number from the first number and store the result in the second number.
  • Subtract the second number (new value) from the first number (new value) and store the result in the first number.
Step 3: Print the swapped values of the two numbers.

Below is the C program to swap two numbers without using a third variable.

//C program to swap two number without third variable
#include <stdio.h>

int main() {
    int num1, num2;

    // Input the two numbers
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Perform arithmetic operations to swap the numbers
    num1 = num1 + num2;
    num2 = num1 - num2;
    num1 = num1 - num2;

    // Print the swapped values
    printf("Swapped values: %d %d\n", num1, num2);

    return 0;
}
Output:
Enter two numbers: 20 10
Swapped values: 10 20

Time Complexity: O(1)
Space Complexity: O(1)

Approach 3: Swap two numbers using Bitwise XOR Operation.

In this approach, we use the bitwise XOR (^) operation to swap two numbers without the need for a temporary variable. The XOR operation between two bits is 1 if the bits are different and 0 if they are the same. By applying XOR between the binary representations of two numbers, we can effectively swap their values. 

Step-by-step algorithm:

Step 1: Input the two numbers from the user.
Step 2: Perform the bitwise XOR operation between the two numbers and store the result in the first number.
Step 3: Perform the bitwise XOR operation between the first number (new value) and the second number and store the result in the second number.
Step 4: Perform the bitwise XOR operation between the first number (new value) and the second number (new value) and store the result in the first number.
Step 5: Print the swapped values of the two numbers.

Below is the C program to swap two numbers using a bitwise operation.
//C program to swap to number using XOR operation
#include <stdio.h>

int main() {
    int num1, num2;

    // Input the two numbers
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Perform bitwise XOR operations to swap the numbers
    num1 = num1 ^ num2;
    num2 = num1 ^ num2;
    num1 = num1 ^ num2;

    // Print the swapped values
    printf("Swapped values: %d %d\n", num1, num2);

    return 0;
}
Output:
Enter two numbers: 8 10
Swapped values: 10 8

Time Complexity: O(1)
Space Complexity: O(1)

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS