C Program Swap Numbers in Cyclic Order Using Call by referance

In this example, we swap value of three variables entered by user in cyclic order.

Swap Numbers in Cyclic Order Using Call by Reference In C Programming

	
#include<stdio.h>
void cyclicSwap(int *a,int *b,int *c);
int main()
{
    int a, b, c;
    printf("Enter a, b and c respectively: ");
    scanf("%d %d %d",&a,&b,&c);
    printf("Value before swapping:\n");
    printf("a = %d \nb = %d \nc = %d\n",a,b,c);
    cyclicSwap(&a, &b, &c);
    printf("Value after swapping:\n");
    printf("a = %d \nb = %d \nc = %d",a, b, c);
    return 0;
}
void cyclicSwap(int *a,int *b,int *c)
{
    int temp;
    // swapping in cyclic order
    temp = *b;
    *b = *a;
    *a = *c;
    *c = temp;
}
	

Output :

	
Enter a, b and c respectively: 10
20
30
Value before swapping:
a = 10
b = 20
c = 30
Value after swapping:
a = 30
b = 10
c = 20
	

Share your thoughts

Ask anything about this examples