Swapping Program in C

Swapping Program in C

Swapping means exchanging the values between the two containers. In the programming world, swapping programs in C user variables instead of real-life containers. Swapping programs accept two inputs and their values. The swapping program in C doesn’t require both the variables to be an integer. It could be float, char, string anything depending upon the user’s requirement. “Swapping means replacing values of a variable with other variables.” To learn more about C Programming and its history, please read this blog first, “C Programming Complete Guide.“ You can watch the video over here, which explains the blog in a simple video with VFX animation.

Write a Program to Swap Two Numbers Using Third Variable in C

To write a Swap Two Number Program in C, a temporary variable is used as the temporary storage. The first content of the variable “a” goes into a temp variable, Then the content of variable “b” in a variable, and Then the content of the temp variable goes to b. If you print the result, the value is swapped.

Declaring Variables in C Program

To start, we will create a program that will be swapping two numbers. Here we will create 3 integers using the keyword int variables a, b, and c. Here, variable c is a temporary variable used as the temporary storage.

main()
{

int a, b, c;

}

Take the user inputs for variable a and use the scanf statement.

main()
{

int a, b, c;
scanf("%d",&a);
scanf("%d",&b);

}

To make it more friendly, we will add a few statements using printf.

main()
{

int a, b, c;
printf("\n Swapping Program");
printf("\nEnter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);

}

Now your program will be more user-friendly to understand users.

Logic to Swapping numbers in C

Let’s implement the Logic to swapping logic in C language. We will assign the value of a to c.

main()
{

int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;

}

Then the value of b into a. 

main()
{

int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;
a=b;

}

Then the value of c into b.

main()
{

int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;
a=b;
b=c;

}

Let’s put a printf statement with respective placeholders and execute the programs.

main()
{

int a, b, c;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("\n After Swapping ");
printf("\n a = %d",a);
printf("\n b = %d",b);

}

I am giving it an input of 10 and then 20. Now press enter. See the result values are swapped.

Output :

Swapping Program Output:

Enter value for a : 10

Enter value for b : 20

After Swapping

a = 20

b = 10

Write a Program to Swap Two Numbers without Using Third Variable in C

Writing swapping logic requires the basic knowledge of constants and variables in C Programming. If you don’t know constants and variables in C Programming, please go back to the book.

Declaring Variables in C Program

Two variables will be used to take the input from the user and swap the values.

main()
{
int a , b;
}

Take the user inputs for variable a and use scanf statement.

main()
{

int a, b;
scanf("%d",&a);
scanf("%d",&b);

}

To make it more user-friendly, we will add a few statements using printf.

main()
{

int a, b;
printf("\n Swapping Program");
printf("\nEnter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);

}

Now your program will be more user-friendly to understand the user.

Logic to Swapping numbers in C

Let’s implement the Logic to swapping logic in C language.

main()
{

int a, b;
printf("\n Swapping Program");
printf("\n Enter value for a : ");
scanf("%d",&a);
printf("\n Enter value for b : ");
scanf("%d",&b);
a = a+b;
b = a-b;
a= a-b;

printf("Value of A is %d and Value of B is %d",a,b);

}
Output : 

Swapping Program

Enter value for a : 5

Enter value for b : 10

Value of A is 10 and Value of B is 5

How to view all topics on C Programming

Watch full programming series on Amazon Prime free with Amazon Prime Membership.

Frequently Asked Questions about Swapping numbers in C Programming

What is Swapping?

Swapping means replacing the content of containers. Containers may be anything like a box, utensils, bottle, etc.
In C Programming, swapping means replacing the values of a variable with other variables. Generally, a variable can store an integer value or a float value or even a string. But swapping without the third variable is used mostly in float and numbers.

What are the practical applications of Swapping in Programming?

Swapping is a very small concept and a few lines of code. But many things are almost impossible without the use of swapping. Especially search algorithms and sort algorithms make use of swapping like bubble sort use swapping concept. In modern programming, it’s used in very specific cases where we need to store the value of a variable temporarily without using persistent space.

Why do we need to learn Swapping logic in C Programming?

Swapping is a very small programming language. You may think it’s not useful. But ask a developer who has worked in core programming for 3-5 Years; he will tell you 100 Places where he would have used swapping logic. In memory-intensive operation, swapping could be used to hold variables temporarily and later swap them in the original variables.

About The Author