Ex.No:6c Swap two numbers using call by reference

AIM:

        To write a C program to swap two numbers using call by reference.

ALGORITHM:

Step 1: Start the program.

Step 2: Set a ← 10 and b ← 20

Step 3: Call the function swap(&a,&b)

Step 3a: Start fuction

Step 3b: Assign t ← *x

Step 3c: Assign *x ← *y

Step 3d: Assign *y ← t

Step 3e: End function

Step 4: Print x and y.

Step 5: Stop the program.

PROGRAM CODE:

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);    // Declaration of function
void main( )
{
    int a = 10, b = 20 ;
    clrscr();
    printf("n  Before swapping");
    printf( "n  a = %d b = %d", a, b );
    swap(&a,&b);    // call by reference
    printf("n  After swapping");
    printf( "n  a = %d b = %d", a, b );
    getch();
}
void swap( int *x, int *y )
{
    int t;
    t = *x;
    *x = *y;
    *y = t;
}
OUTPUT:

value

RESULT:

                    Thus the C program to swap two numbers using call by reference is written and executed successfully.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *