×

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference.

C Program:

#include <stdio.h>
void cyclic_Swap ( int *X, int *Y, int *Z );
int main () 
{
// Considering three variables
    int X, Y, Z;


// Asks user for three inputs
    printf ("Enter the value of X, Y and Z respectively:\n ");
// Storing values in the variables
    scanf ( "%d %d %d", & X, & Y, & Z );


// Printing the input values thoroughly
    printf ( "Before swapping, the values are\n" );
    printf ("X = %d \nY = %d \nZ = %d\n", X, Y, Z);


// Swapping function
    cyclic_Swap ( & X, & Y, & Z );


// Printing the values after succesfully swapping
    printf ( "After swapping, the values are:\n" );
    printf ( "X = %d \nY = %d \nZ = %d", X, Y, Z );


    return 0;
}
// Function
void cyclic_Swap ( int *num1, int *num2, int *num3 ) 
{
    int temp;


    temp = *num2;
    *num2 = *num1;
    *num1 = *num3;
    *num3 = temp;
}

Output:

Enter the value of X, Y and Z respectively:
15
18
12
Before swapping, the values are
X = 15
Y = 18
Z = 12
After swapping, the values are:
X = 12
Y = 15
Z = 18

Likewise, variables X, Y, and Z are used to hold the three numbers that the user input. The cyclic_Swap() function receives the addresses of these numbers as input.

cyclic_Swap ( & X, & Y, & Z );

These addresses have been given to pointers in the function specification for cyclic_Swap().

void cyclic_Swap ( int *num1, int *num2, int *num3 ) 
{
 	.....
}

The values of X, Y, and Z inside main() are also updated when num1, num2, and num3 inside cyclic_Swap() are updated.

Keep in mind: There is no return from the cyclic_Swap() function.


Related Topics

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.