Pages

To Swap Elements Using Call by Reference

Saturday, 29 June 2013
#include<stdio.h>

void cycle(int *x,int *y,int *z);
int main()
{
    int x,y,z;
    printf("Enter value of x, y and z respectively: ");
    scanf("%d%d%d",&x,&y,&z);

    printf("Value before swapping:\n");
    printf("x=%d\ny=%d\nz=%d\n",x,y,z);

    cycle(&x,&y,&z);

    printf("Value after swapping numbers in cycle:\n");
    printf("x=%d\ny=%d\nz=%d\n",x,y,z);
    return 0;
}
void cycle(int *x,int *y,int *z)
{
    int temp;

    temp=*y;
    *y=*x;
    *x=*z;
    *z=temp;
}

No comments:

Post a Comment