# Swap Numbers in a Cyclic Manner in C

## C Program to Swap Numbers in a Cyclic Order

In this page, we have provided codes and algorithms to swap numbers in a cyclic manner in C. This includes swapping two numbers and swapping three numbers in a cyclic order.

## Swap Two Numbers in a Cyclic Order in C

To swap numbers in a cyclic manner in a C program, you can use a temporary variable to store the value of one of the numbers while the other number is being assigned to it.

#### Program to Swap Two Numbers in a Cycle

Run
```#include <stdio.h>

int main() {
int x = 10;
int y = 20;

printf("Before swapping: x = %d, y = %d\n", x, y);

int temp = x;
x = y;
y = temp;

printf("After swapping: x = %d, y = %d\n", x, y);

return 0;
}```

### Output

`Before swapping: x = 10, y = 20After swapping: x = 20, y = 10`

## Swap Three Numbers in a Cyclic Order in C

It is similar to swapping two numbers. The only difference is that there are three temporary variables to assign the values of three numbers.

### Prime Course Trailer

Run
```#include <stdio.h>

int main() {
int x = 10;
int y = 20;
int z = 30;

printf("Before swapping: x = %d, y = %d, z = %d\n", x, y, z);

int temp1 = x;
int temp2 = y;
x = z;
y = temp1;
z = temp2;

printf("After swapping: x = %d, y = %d, z = %d\n", x, y, z);

return 0;
}
```

### Output

`Before swapping: x = 10, y = 20, z = 30After swapping: x = 30, y = 10, z = 20`

### Swap Variables of Different Types in C Program

Run
```#include <stdio.h>

union value {
int i;
float f;
char c;
};

int main() {
int x = 10;
float y = 20.5;

printf("Before swapping: x = %d, y = %f\n", x, y);

union value temp;
temp.i = x;
x = (int) y;
y = (float) temp.i;

printf("After swapping: x = %d, y = %f\n", x, y);

return 0;
}

```

### Output

```Before swapping: x = 10, y = 20.500000
After swapping: x = 20, y = 10.000000```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others