
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Swap Numbers in Cyclic Order Using Call by Reference
Three numbers can be swapped in cyclic order by passing them to a function cyclicSwapping() using call by reference. This function swaps the numbers in a cyclic fashion.
The program to swap numbers in cyclic order using call by reference is given as follows −
Example
#include<iostream> using namespace std; void cyclicSwapping(int *x, int *y, int *z) { int temp; temp = *y; *y = *x; *x = *z; *z = temp; } int main() { int x, y, z; cout << "Enter the values of 3 numbers: "<<endl; cin >> x >> y >> z; cout << "Number values before cyclic swapping..." << endl; cout << "x = "<< x <<endl; cout << "y = "<< y <<endl; cout << "z = "<< z <<endl; cyclicSwapping(&x, &y, &z); cout << "Number values after cyclic swapping..." << endl; cout << "x = "<< x <<endl; cout << "y = "<< y <<endl; cout << "z = "<< z <<endl; return 0; }
Output
The output of the above program is as follows −
Enter the values of 3 numbers: 2 5 7 Number values before cyclic swapping... x = 2 y = 5 z = 7 Number values after cyclic swapping... x = 7 y = 2 z = 5
In the above program, the function cyclicSwapping() swaps the three numbers in cyclic order using call by reference. The function uses a variable temp to do so. The code snippet for this is as follows −
void cyclicSwapping(int *x, int *y, int *z) { int temp; temp = *y; *y = *x; *x = *z; *z = temp; }
In the function main(), the values of the 3 numbers are provided by the users. Then these values are displayed before swapping them. The function cyclicSwapping() is called to swap the numbers and then the values are displayed after swapping them. This is given below −
cout << "Enter the values of 3 numbers: "<<endl; cin >> x >> y >> z; cout << "Number values before cyclic swapping..." << endl; cout << "x = "<< x <<endl; cout << "y = "<< y <<endl; cout << "z = "<< z <<endl; cyclicSwapping(&x, &y, &z); cout << "Number values after cyclic swapping..." << endl; cout << "x = "<< x <<endl; cout << "y = "<< y <<endl; cout << "z = "<< z <<endl;
- Related Articles
- Call by value and Call by reference in Java
- Difference between Call by Value and Call by Reference
- Java program to swap two numbers using XOR operator
- What is call by reference in C language?
- Swap Numbers without using temporary variable in Swift Program?
- C++ Program to Swap Two Numbers
- Java Program to Swap Two Numbers.
- Haskell Program to Swap Two Numbers
- Kotlin Program to Swap Two Numbers
- How to Swap Two Numbers in Swift Program?
- 8085 program to swap two 8 bit numbers using Direct addressing mode
- 8085 program to swap two 16-bit numbers using Direct addressing mode
- 8085 program to swap two 8-bit numbers
- Write a Golang program to swap two numbers without using a third variable
- C program to display numbers in reverse order using single linked list
