Swapping two variables value with out using third variables.

swapping


Swapping is the process of exchanging the value from the main memory to secondary memory for temporary purposes so that the main memory will be free for other purposes. Let's be clear with the given example:-  


         Swapping the value of 3 different numbers number   


Initially,  

 

  a = 5;  b = 10;  c;  

   

  c = a;  

 c  = 5 

  a = b; 

 a  = 10  

  b = c; 

 b  = 5 

   

Now let's see same work in 2 different numbers,  

Initially,  

 a = 10; 

 b = 20;    

 a = a + b  

 a = 10+20 (30) 

 b = a-b  

  b = 30-20 (10)  

 a  = a-b  

  a = 30-10 (20) 


let's be clear with the flowchart  

 Let's see the program here for this,  

 


#include <stdio.h>
#include <conio.h>
void main()
{
    int a = 10b = 20;
    a = a + b;
    b = a - b;
    a = a - b;
    printf("the value of a is  %d \n the value of b is %d ");
    getch();
}

 

OUTPUT 

The value of a is 20 

The value of b is 10 


Follow our page for regular updates.

0 Comments