Swapping the values of two variables is a common operation in programming, and it serves various purposes across different applications. In this article, we are going to swap the values of two variables in C.
Table of Contents
Problem Statement- WAP that swaps values of two variables using a third variable.
Swapping Values of Two Variables by Using Third Variable
The traditional and straightforward method for swapping values involves using a third variable as a temporary storage space. Let’s write a C program that swaps the values of two variables using a third variable.
C Program
#include <stdio.h>
void main()
{
int a = 1, b = 2;
printf("Orignal values : a =%d , b= %d\n ", a, b);
// Swap values using a third variable
int temp = a;
a = b;
b = temp;
printf("Swapped values : a =%d , b= %d\n ", a, b);
}
Output
The above C program performs swapping the values of two variables by using a third variable. As you progress in your programming journey, you may encounter more advanced methods for variable swapping.
Happy Coding
See Also
1 thought on “Swapping Values of Two Variables in C”