[Linux C programming] how to exchange two numbers in Chapter 25 C?

[Abstract] after executing the above procedures, you will understand the exchange of two numbers in C

In C programming language, exchanging two numbers means exchanging the values of two variables. Suppose you have two variables, var1 and var2. var1 has a value of 20 & var2 has a value of 40. Therefore, after the exchange, the value of var1 will become 40 & the value of VaR 2 will become 20. In this blog, you will learn how to exchange two variables in C.

  • Swapping Two Numbers Using Third Variable
  • Swapping Two Numbers Using Without Using Third Variable
  • Swapping Function in C
  • Swap two numbers using pointers in C
  • Swap Two Numbers Using Bitwise XOR

Swap two numbers using the third variable

logic
The idea behind using the third variable to exchange two numbers is simple. Store the value of the first variable in a temporary variable. Store the value of the second variable in the first variable. Finally, the value of the temporary variable is stored in the second variable. In this program, we use a temporary variable to hold the value of the first variable.

  • Assign var1 value to temporary variable: temp = var1

  • Assign var2 value to var1: var1 = var2

  • Assign temporary values to var2: var2 = temp

code:

temp = var1;
var1 = var2;
var2 = temp;

Now, let's look at the complete code.

example

#include <stdio.h> 
int main()
{
	int var1, var2, temp; 
	printf("Enter two integersn");
	scanf("%d%d", &var1, &var2);
	printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
	temp = var1;
	var1 = var2;
	var2 = temp;
	printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
	return 0;
}

Output:

Continue this article on exchanging two numbers in C

Exchange two numbers without using the third variable

In this variant of exchanging two variables, we do not use any temporary variables to store values. In the first variable, we store the sum of the two variables. Then, in the next step, we will extract the value of the first variable by subtracting the value of the second variable from the sum and storing it in the second variable. Finally, we extract the original value ND variable of 2 & which is stored in the 1-day variable.

code:

var1 = var1 + var2;
var2 = var1 - var2;
var1 = var1 - var2;

Now, let's look at the complete code.

example

#include <stdio.h>
int main()
{
	int var1, var2, temp;
	printf("Enter two integersn");
	scanf("%d%d", &var1, &var2);
	printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
	var1 = var1 + var2;
	var2 = var1 - var2;
	var1 = var1 - var2;
	printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
	return 0;
}

Output:

Continue this article on exchanging two numbers in C

Exchange function in C

You can create a swap function in C to implement any swap variant. When you want to swap two variables, you can call the function at any time. Since we want to modify the local variables of main through the exchange function, we must use pointers to modify them.

example

void swap(int *num1, int *num2) 
{ 
	int temp = *num1; 
	*num1 = *num2; 
	*num2 = temp; 
}

int main() 
{ 
	int var1, var2; 
	printf("Enter Value of var1 "); 
	scanf("%d", &var1); 
	printf("nEnter Value of var2 "); 
	scanf("%d", &var2); 
	swap(&var1, &var2); 
	printf("nAfter Swapping: var1 = %d, var2 = %d", var1, var2); 
	return 0; 
}

Output:

Moving to the next variant, we use pointers to swap two numbers.

Exchange two numbers with the pointer in C

You can also swap two variables using pointers, where the address of the variable is passed to two different variables. Then, exchange their values.

code

num1 = &var1;
num2 = &var2;
temp = *num2;
*num2   = *num1;
*num1   = temp;

Now, let's look at the complete code.

example

#include <stdio.h> 

int main()
{
	int var1, var2, *num1, *num2, temp;
	printf("Enter the value of var1 and var2n");
	scanf("%d%d", &var1, &var2);
	printf("Before Swappingnvar1 = %dnvar2 = %dn", var1, var2);
	num1 = &var1;
	num2 = &var2;
	temp = *num2;
	*num2   = *num1;
	*num1   = temp;
	printf("After Swappingnvar1 = %dnvar2 = %dn", var1, var2);
	return 0;
}

Output:

Go to the last variant of the swap variable in C. Let's learn how to swap two variables using the bitwise XOR operator.

Swap two numbers using bitwise XOR

The XOR operator works similarly to swapping without temporary variables. We extract and calculate the XOR of two variables. Then we extract individual values and exchange them.

Assume var1 = 20 and var2 = 40. Binary values 20 = 10100 and 40 = 101000.

example:

Var1 = Var1 ^ Var2
Var1 = 10100 ^ 101000
Var1 = 111100

Var2 = Var1 ^ Var2
Var2 = 111100 ^ 101000
Var2 = 10100

Var1 = Var1 ^ Var2;
Var1 = 111100 ^ 10100
Var1 = 101000
The final value after exchanging 2 numbers: var1 = 40 and var2 = 20

var1 = var1 ^ var2;
var2 = var1 ^ var2;
var1 = var1 ^ var2;

Now, let's look at the complete code.

#include <stdio.h>

int main()
{
	int var1, var2, temp; 
	printf("Enter two integersn");
	scanf("%d%d", &var1, &var2);
	printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2); 
	var1 = var1 ^ var2;
	var2 = var1 ^ var2;
	var1 = var1 ^ var2;
	printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
	return 0;
}

Output:

Now, after completing the above procedure, you will learn about all variants of exchanging two numbers in C. I hope this blog can provide you with information and add value.

[copyright notice] this article is the original content of users in Huawei cloud community. When reprinting, you must mark the source of the article (huaweiyun community), the article link, the author and other basic information, otherwise the author and the community have the right to investigate the responsibility. If you find any content suspected of plagiarism in the community, you are welcome to send an email to: cloudbbs@huaweicloud.com Report and provide relevant evidence. Once verified, the community will immediately delete the suspected infringement content.

Added by greeneel on Mon, 03 Jan 2022 17:04:19 +0200