C program to Swap Two numbers - CodeMub

Introduction:

In the world of programming, manipulating data is an essential task. One common operation is swapping the values of two variables. Swapping two numbers is a fundamental programming exercise that helps in various algorithms and applications. In this blog, we will explore a simple and efficient way to swap two numbers using the C programming language. We will discuss the logic behind the swapping process and provide a code example to demonstrate its implementation.

Understanding the Swapping Logic:

Swapping two numbers involves exchanging their values. Let's assume we have two variables, a and b, and we want to swap their values. The basic idea is to use a third temporary variable, let's call it temp, to hold the value of one variable while we perform the swapping operation.

The swapping process follows these steps:

  • Store the value of a in the temporary variable temp.
  • Assign the value of b to a, effectively replacing the original value of a.
  • Assign the value of temp to b, thus transferring the original value of a to b.
By the end of these steps, a will contain the initial value of b, and b will hold the initial value of a. This simple logic allows us to interchange the values of two variables.

In C, there are multiple ways to swap two numbers, and we will explore two methods: one using a temporary variable and another without using a temporary variable. 

Let's dive into each method: 

 Swapping with a Temporary Variable 

 In this method, we use an additional variable to temporarily hold one of the numbers while we interchange their values.

 Here's the C program to swap two numbers using a temporary variable:
#include
int main() {
double a, b;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
// swapping using a temporary variable
double temp = a;
a = b;
b = temp;
printf("After swapping, a = %.2lf\n", a);
printf("After swapping, b = %.2lf", b);
return 0;
}
To explain the code, we start by declaring two double variables `a` and `b`, which will hold the input values from the user. The `printf` and `scanf` functions are used to prompt the user for input and read the values accordingly. 
 Next, we introduce a temporary variable `temp` to hold the value of `a`. By assigning `a` to `temp`, we preserve the original value of `a` for later use. Then, we assign the value of `b` to `a` and finally assign the value of `temp` (the original `a`) to `b`. As a result, the values of `a` and `b` are effectively swapped. 

 Finally, we display the swapped values of `a` and `b` using the `printf` function. 

Swapping without a Temporary Variable

 In this method, we swap the values of `a` and `b` without using an extra variable. 
Instead, we utilize arithmetic operations to perform the swapping. 

Here's the C program to swap two numbers without using a temporary variable:
#include
int main() {
double a, b;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
// swapping without a temporary variable
a = a - b;
b = a + b;
a = b - a;
printf("After swapping, a = %.2lf\n", a);
printf("After swapping, b = %.2lf", b);
return 0;
}
In this code, after obtaining the values of `a` and `b` from the user, we perform the swapping using arithmetic operations. Initially, we subtract `b` from `a` and assign the result back to `a`. 

This step stores the difference between the original values of `a` and `b` in `a`. Next, we add the original value of `b` to the updated value of `a` and assign the sum to `b`. Now, `b` holds the original value of `a`. 

Finally, by subtracting the updated value of `a` from `b`, we obtain the original value of `b`, which we assign to `a`. Consequently, the values of `a` and `b` are swapped. 

Both methods achieve the same result of swapping two numbers, but they differ in the approach used. Whether you opt for a temporary variable or perform the swapping directly, understanding these techniques can be beneficial in various programming scenarios. 

Conclusion

In this blog, we explored two C programs to swap two numbers, one using a temporary variable and the other without using a temporary variable. These methods provide different approaches to accomplish the same task. Remember to choose the method that best suits your specific requirements. Happy coding!
Next Post Previous Post
No Comment
Add Comment
comment url