top of page

C Program Swap Numbers in Cyclic Order Using Call by Reference

  • Writer: Jino Shaji
    Jino Shaji
  • Jan 1, 2015
  • 1 min read

Updated: Jun 9, 2020


This program takes three enters from user which is stored in variable a, b and c respectively. Then, these variables are passed to function using call by reference. This function swaps the value of these elements in cyclic order.

C Program to Swap Elements Using Call by Reference



#include
void Cycle(int *a,int *b,int *c);
int main(){
 int a,b,c;
    printf("Enter value of 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);
 Cycle(&a,&b,&c);
    printf("Value after swapping numbers in cycle:\n");
    printf("a=%d\nb=%d\nc=%d\n",a,b,c);
 return 0;
}
void Cycle(int *a,int *b,int *c){
 int temp;
    temp=*b;
 *b=*a;
 *a=*c;
 *c=temp;
}
Enter value of a, b and c respectively: 1
2
3
Value before swapping: 
a=1
b=2
c=3
Value after swapping numbers in cycle:
a=3
b=1
c=2

Recent Posts

See All
Pointers In C

C Pointers Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks,...

 
 
 

Comments


bottom of page