×

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

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute 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.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.