Welcome to our post. Today, we’re going to talk about c program to swap two numbers in a simple and helpful way so that you can understand everything clearly without any confusion so stay tuned
For instance, input: a = 15, b = 10
Results: a = 10, b = 15
This is one of those programs that can be thought of as one of the foundational elements that will help you hone your coding abilities. In order to increase your flexibility with variable manipulation in C programming, we'll go over swapping techniques using different approaches in this article.
Results: a = 10, b = 15
This is one of those programs that can be thought of as one of the foundational elements that will help you hone your coding abilities. In order to increase your flexibility with variable manipulation in C programming, we'll go over swapping techniques using different approaches in this article.
Method for Changing Two Numbers in C
The following are the methods that will be covered in this article for swapping two numbers in C:1. Using a Temporary or Third Variable to Swap Two Numbers
This is the simplest and most popular way to switch two numbers. To hold the value of one variable while swapping, a temporary third variable is first introduced. For instance, the value of A is stored in the temporary variable C, followed by the assignment of the value of B to A and the stored value to B. This method of exchanging values is very easy.
2. Change Without a Third Variable
This method is based on a series of arithmetic operations, either multiplication and division or addition and subtraction. For instance, A stores the sum of A and B, and basic equations are used to retrieve the initial values. This technique doesn't require additional memory.
3. The C Swapping Function
This approach makes the code reusable by encapsulating the swapping logic in a function. The function will swap the values of the two variables you pass as arguments. This method greatly improves the clarity of your program.
4. Using Pointers in C to Swap Two Numbers
An additional degree of flexibility is provided by pointers. Here, variables are passed to a function via their memory addresses rather than directly, and the function uses these addresses to directly modify the values of the variables.
C Program to Swap Two Numbers Using Third Variable
#include <stdio.h>
int main()
{
int var1, var2, temp;
printf("Enter two integers \n");
scanf("%d%d", &var1, &var2);
printf("Before Swappingn First variable = %d\nSecond variable = %d \n", var1, var2);
temp = var1;
var1 = var2;
var2 = temp;
printf("After Swappingn First variable = %d\nSecond variable = %d\n", var1, var2);
return 0;
}
Output:
Enter two integers
2 3
Before Swapping, First variable = 2
Second variable = 3
After Swapping, First variable = 3
Second variable = 2
The first variable's value is assigned to the temp variable in the program mentioned above. The second variable is then given the value of the first variable.
Lastly, the second is given the temp, which contains the first's initial value. The swapping process is now complete.
Lastly, the second is given the temp, which contains the first's initial value. The swapping process is now complete.
C Program to Swap Two Numbers Without Using Third Variable
Two numbers can be switched without the need for a third variable. To switch two numbers without utilizing a third variable, there are three typical methods:
#include <stdio.h>
int main()
{
int var1, var2, temp;
printf("Enter two integers\n");
scanf("%d%d", &var1, &var2);
printf("Before Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2);
var1 = var1 + var2;
var2 = var1 - var2;
var1 = var1 - var2;
printf("After Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2);
return 0;
}
Output:
Enter two integers
5 6
Before Swapping
First variable = 5
Second variable = 6
After Swapping
First variable = 6
Second variable = 5
C Program to Swap Two Numbers Using Pointers
#include <stdio.h>
int main()
{
int var1, var2, *num1, *num2, temp;
printf("Enter the value of var1 and var2\n");
scanf("%d%d", &var1, &var2);
printf("Before Swapping\nvar1 = %d\nvar2 = %d\n", var1, var2);
num1 = &var1;
num2 = &var2;
temp = *num2;
*num2 = *num1;
*num1 = temp;
printf("After Swapping\nvar1 = %d\nvar2 = %d\n", var1, var2);
return 0;
}
Output:
Enter the value of var1 and var2
1 2
Before Swapping
var1 = 1
var2 = 2
After Swapping
var1 = 2
var2 = 1
Conclusion
As you can see, there are various ways to write the C program that swaps two numbers. Pointers, arithmetic operations, or a third variable can all yield the same outcome. To strengthen your fundamental abilities, it's crucial to practice these codes.

Post a Comment