Swapping of Two Numbers Using Call By Reference in C

include

swap (int *, int *);
int main()
{
int a, b;
printf(“\nEnter value of a & b: “);
scanf(“%d %d”, &a, &b);
printf(“\nBefore Swapping:\n”);
printf(“\na = %d\n\nb = %d\n”, a, b);
swap(&a, &b);
printf(“\nAfter Swapping:\n”);
printf(“\na = %d\n\nb = %d”, a, b);
return 0;
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

OUTPUT:
Enter value of a & b: 10 20

Before Swapping:

a = 10

b = 20

After Swapping:

a = 20

b = 10

Leave a Reply

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