Friday, September 23, 2011

WAP to swap two numbers using call by reference method


/*
  Name: WAP to swap two numbers using call by reference method
  Author: Parveen Malik
  Date: 23/09/11 11:43
*/


#include <stdio.h>
#include <conio.h>

void swap(int*,int*);


int main()
{
    int x,y;
    printf("Enter the value of x and y \n");
    scanf("%d%d",&x,&y);
    printf("Before swapping\nx = %d\ny = %d\n",x,y);
    swap(&x,&y);
    printf("After swapping\nx = %d\ny = %d",x,y);
    getch();
    return 0;
}


void swap (int *a, int *b)
{
     int temp;
     temp=*a;
     *a=*b;
     *b=temp;
     }


OUTPUT : 


     

WAP to swap two numbers without using third variable


/*
  Name: WAP to swap two numbers without using third variable
  Author: Parveen Malik
  Date: 23/09/11 11:34
*/
#include <stdio.h>
#include <conio.h>
 int main()
 { 
     int x,y;
     printf("Enter the value of x and y \n");
     scanf("%d%d",&x,&y);
     printf("Before swapping\nx = %d\ny = %d",x,y);
     x=x+y;
     y=x-y;
     x=x-y;
     
     printf("\nAfter swapping\nx = %d\ny = %d\n",x,y);
     getch();
             return 0;
             }

OUTPUT : 

WAP to swap two numbers using third variable


/*
  Name: WAP to swap two numbers using third variable
  Author: Parveen Malik
  Date: 23/09/11 10:50
*/


#include <stdio.h>
#include <conio.h>


int main()
{
    int x,y,temp;
    printf("Enter the value of x and y \n");
    scanf("%d%d",&x,&y);
    printf("Before swapping \nx = %d\ny = %d",x,y);
    temp=x;
    x=y;
    y=temp;
    printf("\nAfter swapping \nx = %d\ny = %d",x,y);
    getch();
    return 0;
}


OUTPUT :