C Program to Swap Two Numbers Without Using a Temporary Variable

Introduction

Swapping two numbers without using a temporary variable is a common programming exercise that involves arithmetic operations. This guide will show you how to write a C program to swap the values of two variables without using an additional temporary variable.

Problem Statement

Create a C program that:

  • Takes two numbers as input from the user.
  • Swaps the values of the two numbers without using a temporary variable.
  • Displays the numbers after swapping.

Example:

  • Input: a = 5, b = 10
  • Output: a = 10, b = 5

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the two numbers.
  4. Input the Numbers: Use scanf to take input from the user for the two numbers.
  5. Swap the Numbers Using Arithmetic Operations: Use addition and subtraction (or XOR operation) to swap the values of the two numbers without a temporary variable.
  6. Display the Swapped Values: Use printf to display the numbers after swapping.

C Program (Using Addition and Subtraction)

#include <stdio.h> /** * C Program to Swap Two Numbers Without Using a Temporary Variable * Author: https://www.javaguides.net/ */ int main() { // Step 1: Declare variables to hold the numbers int a, b; // Step 2: Prompt the user to enter the first number printf("Enter the first number (a): "); scanf("%d", &a); // Step 3: Prompt the user to enter the second number printf("Enter the second number (b): "); scanf("%d", &b); // Step 4: Swap the numbers using addition and subtraction a = a + b; // Step 4.1: Add b to a and store the result in a b = a - b; // Step 4.2: Subtract b from the new a (which is a+b) to get the original a and assign it to b a = a - b; // Step 4.3: Subtract the new b (original a) from the new a (which is a+b) to get the original b and assign it to a // Step 5: Display the swapped values printf("After swapping, a = %d, b = %d\n", a, b); return 0; // Step 6: Return 0 to indicate successful execution } 

C Program (Using XOR Operation)

#include <stdio.h> /** * C Program to Swap Two Numbers Without Using a Temporary Variable (Using XOR) * Author: https://www.javaguides.net/ */ int main() { // Step 1: Declare variables to hold the numbers int a, b; // Step 2: Prompt the user to enter the first number printf("Enter the first number (a): "); scanf("%d", &a); // Step 3: Prompt the user to enter the second number printf("Enter the second number (b): "); scanf("%d", &b); // Step 4: Swap the numbers using XOR operation a = a ^ b; // Step 4.1: XOR a and b, store the result in a b = a ^ b; // Step 4.2: XOR the new a (which is a^b) with b to get the original a and assign it to b a = a ^ b; // Step 4.3: XOR the new a (which is a^b) with the new b (original a) to get the original b and assign it to a // Step 5: Display the swapped values printf("After swapping, a = %d, b = %d\n", a, b); return 0; // Step 6: Return 0 to indicate successful execution } 

Explanation

Using Addition and Subtraction

  • Step 4.1: Add a and b and store the result in a. Now, a holds the sum of a and b.
  • Step 4.2: Subtract b from the new value of a to get the original value of a, and assign it to b.
  • Step 4.3: Subtract the new value of b (which now holds the original a) from the new value of a to get the original value of b, and assign it to a.

Using XOR Operation

  • Step 4.1: XOR a and b, store the result in a. Now a holds the XOR of a and b.
  • Step 4.2: XOR the new value of a with b to get the original value of a, and assign it to b.
  • Step 4.3: XOR the new value of a (which is the XOR of original a and b) with the new value of b (original a) to get the original value of b, and assign it to a.

Step 5: Display the Swapped Values

  • The swapped values of a and b are displayed using the printf function.

Step 6: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example (Using Addition and Subtraction):

Enter the first number (a): 5 Enter the second number (b): 10 After swapping, a = 10, b = 5 

Example (Using XOR Operation):

Enter the first number (a): 5 Enter the second number (b): 10 After swapping, a = 10, b = 5 

Conclusion

This C program demonstrates how to swap the values of two variables without using a temporary variable. Both the addition-subtraction method and the XOR operation method are efficient and avoid the need for extra storage, making them useful techniques in various programming scenarios.

Leave a Comment

Scroll to Top