C program to swap two numbers without using third variable

 C Program To Swap Two Numbers Without Using Third Variable


Today we will learn how to swap two numbers without using third variable. We are using two variables a and b, first we will accept two numbers from user and store it in a and b respectively. Then we will add a and b and store it in a, then substract two numbers and store in b and finally again substract two numbers and store in a.

C program to swap two numbers without using third variable
C program to swap two numbers without using third variable

Algorithm:-

  1. Start
  2. Read two numbers from user
  3. Add first number and second number and store it in first number
  4. Subtract first number and second number and store it in second number
  5. Again subtract first number and second number and store it in first number
  6. Print the result 
  7. Stop


For Example:-

If a=15 and b=23, then
a=15+23=38 and b=38-23=15 and finally a=38-15=23
so final output will be
a=23 and b=15.

Let us consider another example, say c = 45 and d = 25
In first step we do c = 45 + 25 = 70 and 
In the second step we do d = 70 - 25 = 45 and lastly 
In the third and final step we do c = 70 - 45 = 25 

As you can see in the above example by using this method we can easily swap two numbers without any temporary variable.

If you want to read the program to swap two numbers using a temporary variable then click here.


Program:-

Here, in this program first we input the first number and store it in 'a' and then we input second number and store that in 'b'. Then we use the method as we have done in the example above that is first we add two numbers and store the variable in 'a' and then we subtract the value stored in variable 'a' and the value of variable b and then in the final step we subtract the value stored in 'a' with the value stored in variable 'b'. After doing that we get the numbers swapped without using a temporary variable and then we print the output on the screen. Refer the following program

#include <stdio.h>

int main()
{
    int a, b;
    printf("Enter first number: ");
    scanf("%d", &a);
    
    printf("Enter second number: ");
    scanf("%d", &b);
    
     printf("Before Swapping:\n a=%d and b=%d\n", a, b);
    
    a=a+b;
    b=a-b;
    a=a-b;
    
    printf("After Swapping:\n a=%d and b=%d", a, b);

    return 0;
}

Output:-

Enter first number: 12                                                                                                
Enter second number: 87                                                                                               
Before Swapping:                                                                                                      
 a=12 and b=87                                                                                                        
After Swapping:                                                                                                       
 a=87 and b=12

Enter first number: 15                                                                                                
Enter second number: 23                                                                                               
Before Swapping:                                                                                                      
 a=15 and b=23                                                                                                        
After Swapping:                                                                                                       
 a=23 and b=15 

Post a Comment

0 Comments