Overview of Swapping in C
C language is the base language for all programming languages. Like other programming languages, there are various inbuilt functions available in C language. Inbuilt functions are used to solve complex problems quickly and make code easy. In order to built-in functions, C language also allows us to create customized functions to develop logic. In this article, we are going o discussed how to swap numbers or variables in C language. It means exchanging two numbers or variables with another using C language syntax.
How to Swap Numbers in C?
Suppose there are two variables, A and B. Variable A exchanges its data with variable B and variable B exchange its data with B. Swapping can be done by using two variables or three variables, it depends on the requirement. In this section, we are going to discussed how to swap two numbers in C and three numbers in C.
Swap Two Numbers in C
In this section, we are going to discussed how to swap two numbers in C language with the help of example and explanation.
Example: In the following C program, the user can enter 2 numbers he wishes to swap, then the result will be displayed on the screen. The program for swapping two numbers in C is as follows.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int first_number, second_number, temp;
printf("Enter first number: "); //allow user to add first number
scanf("%d",&first_number);
printf("Enter second number: "); //allow user to add second number
scanf("%d",&second_number);
printf("Before swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
temp = first_number; //first number is assigned to temp
first_number = second_number; //second number is assigned to first number
second_number = temp; //first number is assigned to secind number
printf("After swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
return 0;
}
Output:
Explanation of the above program: To swap two numbers, first, we initialize two variables i.e. first_number and second_number. With these two numbers, a temporary variable named temp is also initialized to store a number temporarily. Then scan function allows the user to assigned numbers according to their wish. Then to swap numbers, we use the temp variable for storing numbers temporarily. First, we transfer first_number to temp variable and make first_number empty. As first_number is empty, we assigned second_number to the first number. Like this second_number is transferred to first_number. In the end, as a second variable is now empty, we transfer first_number that is stored in the temp variable is assigned to second. Like this, we transferred first_number to second. This is how we swap two numbers using the temp variable.
Comments are used to help people to understand the code easily.
Swap Three numbers in C
In this section, we are going to discussed how to swap 3 numbers in C language with the help of example and explanation.
Example: In the following C program, the user can enter 3 numbers he wishes to swap, then the result will be displayed on the screen. Program for swapping two numbers in C. Program for swapping three numbers in C is as follows:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int first_number, second_number, third_number, temp;
printf("Enter first number: "); //allow user to add first number
scanf("%d",&first_number);
printf("Enter second number: "); //allow user to add second number
scanf("%d",&second_number);
printf("Enter third number: "); //allow user to add third number
scanf("%d",&third_number);
printf("Before swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
printf("Third number: %d \n", third_number);
temp = first_number; //first number is assigned to temp
first_number = second_number; //second number is assigned to first number
second_number = third_number; //third number is assigned to second number
third_number = temp; //first number is assigned to third number
printf("After swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
printf("Third number: %d \n", third_number);
return 0;
}
Output:
Explanation of the above program: The concept for swapping three numbers is the same as two numbers only difference is exchanging numbers. To swap three numbers, first, we initialize three variables i.e. first_number, second_number, and third_number. With these three numbers, a temporary variable named temp is also initialized to store a number temporarily. Then scan allows the user to assigned numbers according to their wish. Then to swap numbers, we use the temp variable for storing numbers temporarily. First, we transfer the first number to the temp variable and make the first number empty. As the first number is empty, we assigned the second number to the first number and make the second number empty. This second number is transferred to the first number. As the second number is empty, we assigned the third number to the second number. Then this third number is transferred to the second number. In the end, as a third number is now empty, we transfer the first number that is stored in the temp variable is assigned to third. This is how we swap 3 numbers using the temp variable.
Recommended Articles
This is a guide to Swapping in C. Here we discuss how the use of C programming language could be swapped using two variables with appropriate code and output. You can also go through our other related articles to learn more –