In this article, we are going to discuss how to swap two numbers by using the function or without a function. So let’s start!
There are two ways to swap the number one is by using a third variable and another by adding or subtracting the values in it. So we going to discuss them one by one.
C program to Swap two numbers with function or without function
Method 1: C program to swap two numbers using function.
// C program to swap two numbers by using the function
#include<stdio.h>
int swap(int x, int y){
int temp =x;
x = y;
y = temp;
}
int main(){
int x, y;
printf(“Enter the value of x :”);
scanf(“%d”, &x);
printf(“Enter the value of y :”);
scanf(“%d”, &y);
swap(x,y);
printf(“After swapping the number are x=%d y=%d”,x,y);
return 0;
}
output :
Enter the value of x:22
Enter the value of y:33
After swapping the number are x=22 y=33
Step 1: First create a user-defined function and pass two parameters in it.
Step 2: After passing the argument we simply use the third variable in it and give the name as temp then we simply do – int temp =x;
x = y;
y = temp;
Step 3: After this, we simply go for the main method, and then simply we create two int-type variables to get input from the user.
Step 4: After that, we simply use scanf() to store the value of a variable.
Step 5: then we simply swap(x,y).
step 6: after the above steps we simply use printf() to get our desired output.
Method 2: C program to swap two numbers without using function.
#include<stdio.h>
int main(){
int num1, num2, temp;
printf(“Enter value for num1 :”);
scanf(“%d”, &num1);
printf(“Enter value for num2 :”);
scanf(“%d”, &num2);
temp = num1;
num1 = num2;
num2 = temp;
printf(“After swapping the number num1=%d, num2=%d”, num1,num2);
return 0;
}
Output
Enter a value for num1:22
Enter a value for num2:33
After swapping the numbers num1=33, num2=22
Step 1: First we take the int type variable to get input from user. And then also we use int type temp variable to store the value of x in it.
Step 2: After getting we simply use scanf() to store the value of variables.
Step 3: then we simply swap two variables such as temp = num1;
num1 = num2;
num2 = temp;
Step 4: After that, we simply use printf() to print our desired output.
Method 3: C program to swap two numbers without using a third variable
// C program to swap two numbers without using a third variable.
#include<stdio.h>
int main(){
int num1, num2;
printf(“Enter a value for num1 :”);
scanf(“%d”, &num1);
printf(“Enter a value for num2 :”);
scanf(“%d”, &num2);
num1 = num1 + num2;
num2 = num1 – num2;
num1 = num1 – num2;
printf(“After swapping the value num1 = %dn”, num1);
printf(“After swapping the value num2 = %d”, num2);
return 0;
}
Output
Enter a value for num1:22
Enter a value for num2:11
After swapping the numbers are num1=11, num2=22
Enter a value for num1:22
Enter a value for num2:33
After swapping the numbers num1=33, num2=22
Step 1: First we take the int type variable to get input from the user.
Step 2: After getting we simply use scanf() to store the value of variables.
Step 3: then we simply swap two variables such as num1 = num1 + num2;
num2 = num1 – num2;
num1 = num1 – num2;
Step 4: After that, we simply use printf() to print our desired output.
In this way, we simply understand how we swap two numbers by using these three methods. I hope this thing is clear to you. So for now keep learning and keep exploring.