C Program Swap Numbers in Cyclic Order using Call by Reference using Function
ADVERTISEMENTS
C program swap numbers in cyclic order using call by reference using function. In this article, you will learn how to swap numbers in cyclic order using call by reference in c language using function.
Source Code
// C Program Swap Numbers in Cyclic Order using Call by Reference using Function
#include <stdio.h>
void swapCycle(int *p, int *q, int *r);
// It's the driver function
int main() {
int p, q, r;
// p, q & r - denotes the input numbers to swap in cyclic order
printf("-----Enter the three numbers one by one-----\n");
scanf("%d %d %d", &p, &q, &r);
printf("\n-----Values before swapping the numbers-----\n");
printf("p = %d \nq = %d \nr = %d\n", p, q, r);
swapCycle(&p, &q, &r);
printf("\n\n-----Values after swapping the numbers-----\n");
printf("p = %d \nq = %d \nr = %d\n", p, q, r);
return 0;
}
// This function will swap the numbers itself
void swapCycle(int *x, int *y, int *z) {
int temp;
// swapping in cyclic order
temp = *y;
*y = *x;
*x = *z;
*z = temp;
}
Output
-----Enter the three numbers one by one-----
2
5
7
-----Values before swapping the numbers-----
p = 2
q = 5
r = 7
-----Values after swapping the numbers-----
p = 7
q = 2
r = 5