Skip to content
Home » C Program to swap Numbers in Cyclic Order using Pointer

C Program to swap Numbers in Cyclic Order using Pointer

Learn about C Program to swap Numbers in Cyclic Order using Pointer in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C Program to swap Numbers in Cyclic Order using Pointer

Here we declare pointer to variables and define function to swap Ex: Integers 1 3 7 after swapping will change to 7 1 3.As pointers are used, we will use call by reference.

#include<stdio.h>
void swap(int *p, int *q, int *r);
int main()
{
  int a, b, c;
  int *pa, *pb, *pc;//declaring pointers
  pa=&a, pb=&b, pc=&c;//assigning variables
  printf("Enter three integer: ");
  scanf("%d %d %d",pa,pb,pc);
  printf("Value Before Swapping:\n");
  printf("a=%d \t b=%d \t c=%d\n", *pa, *pb, *pc);
  swap(pa,pb,pc);//calling function for swapping
  printf("Value After Swapping:\n");
  printf("a=%d \t b=%d \t c=%d\n", *pa, *pb, *pc);
  return 0;
}
void swap(int *p, int *q, int *r)//declare swap function
{
  int temp;
  temp = *p;
  *p = *r;
  *r = *q;
  *q = temp;
}

Output:

Enter three integer: 1 3 7
Value Before Swapping:
a=1 b=3 c=7
Value After Swapping:
a=7 b=1 c=3

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program to find Character is vowel or consonant using Pointer
Find Sum & average in Range using Pointer & Function