Algorithm


Include Header File:
  Include the standard input/output header file (#include <stdio.h>).

Function Prototype:
  Declare a function prototype for cyclicSwap that takes three integer pointers as parameters.

Main Function:
  Declare three integer variables a, b, and c.
  Prompt the user to enter values for a, b, and c.
  Display the initial values of a, b, and c.

Function Call:
  Call the cyclicSwap function, passing the addresses of variables a, b, and c.

Display Swapped Values:
  Display the values of a, b, and c after the cyclic swap operation.

Cyclic Swap Function:
  Declare a function cyclicSwap that takes three integer pointers as parameters.
  Use a temporary variable to swap the values in a cyclic order.
  Update the values at the memory locations pointed to by the input pointers.

Return:
  Return 0 to indicate successful completion of the program.

Code Examples

#1 Code Example-C Programing to Swap Elements Using Call by Reference

Code - 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 *n1, int *n2, int *n3) {
    int temp;
    // swapping in cyclic order
    temp = *n2;
    *n2 = *n1;
    *n1 = *n3;
    *n3 = temp;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a, b and c respectively: 1
2
3
Value before swapping:
a = 1
b = 2
c = 3
Value after swapping:
a = 3
b = 1
c = 2
Advertisements

Demonstration


C Programing Example Swap Numbers in Cyclic Order Using Call by Reference-DevsEnv

Next
Appending into a File in C Programming