Pages

This blog is under construction

Monday, December 10, 2018

C program to swap two numbers.


#include<stdio.h>

void swap(int *, int *);
int main()
     {
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("process one: a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\nprocess two a= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\nprocess three a= %d  b=  %d",a,b);
 
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\n process four a= %d  b=  %d",a,b);
 
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\nprocess five a= %d  b=  %d",a,b);
 
 
// process six
    a=5,b=10;
     swap(&a,&b);
     printf("\nprocess six: a= %d  b=%d",a,b);
}

void swap(int *x, int *y)
  {
      int temp=*x;*x=*y;*y=temp;
   
  }


Output:
process one: a= 10  b=  5                                                                                                                              
process two a= 10  b=  5                                                                                                                               
process three a= 10  b=  5                                                                                                                             
process four a= 10  b=  5                                                                                                                             
process five a= 10  b=  5                                                                                                                              
process six: a= 10  b=5   


No comments:

Post a Comment