Program for SWAPING OF 2 NUMBERS USING CALL BY VALUE ,CALL BY REFERENCE

#include<stdio.h> #include<conio.h> swap(int x,int y); swap(int *x,int *y); main() {           int a,b,ch;        ...

#include<stdio.h>
#include<conio.h>
swap(int x,int y);
swap(int *x,int *y);
main()
{           int a,b,ch;
            clrscr();
            printf("ENTER A,B VALUES\n");
            scanf("%d%d",&a,&b);
            printf("BEFORE SWAPPING \n");
            printf("A=%d,B=%d\n",a,b);
            printf("1:USING CALL BY VALUE \n 2:USING CALL BY REFERENCE\n");
            scanf("%d",&ch);
            switch(ch)
            {
                        case 1:swap(a,b);
                                     break;
                        case 2:
swap(&a,&b);
                                     break;
                        default:
printf("INVALID");
            }
            printf("AFTER SWAPPING \n");
            printf("A=%d,B=%d\n",a,b);
            getch();
}

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

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


OUTPUT :

            ENTER A,B VALUES  10  20
            BEFORE SWAPPING        A=10   B=20
            1:USING CALL BY VALUE
            2:USING CALL BY REFERENCE
            2
            AFTER SWAPPING
            A=20

            B=10

Related

C Programs 7394810786006091791

Post a Comment

emo-but-icon

item