This post contains a total of 9+ C Program Examples with Source Code to Swap Numbers. All these programs to Swap Numbers are made using C Programming Language.
You can use the source code of these examples with credits to the original owner.
Related Posts
C Programs to Swap Numbers
1. By Gaurav Sharma
Made by Gaurav Sharma. Program to swap two number without third variable. Source
enter two number 2 3 number before swap a=2 b=3 number after swap a=3 b=2
#include <stdio.h>
int main()
{
int a,b;
printf("enter two number\n");
scanf("%d %d",&a,&b);
printf("number before swap\na=%d b=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("number after swap\na=%d b=%d",a,b);
return 0;
}
2. By Wajeeh
Made by Wajeeh. Simple C Number Swap Program. Source
Enter first number: 1 Enter second number: 2 After swapping, firstNumber = 2.00 After swapping, secondNumber = 1.00
#include <stdio.h>
int main()
{
double firstNumber, secondNumber, temporaryVariable;
printf("Enter first number: ");
scanf("%lf", &firstNumber);
printf("\nEnter second number: ");
scanf("%lf",&secondNumber);
// Value of firstNumber is assigned to temporaryVariable
temporaryVariable = firstNumber;
// Value of secondNumber is assigned to firstNumber
firstNumber = secondNumber;
// Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
secondNumber = temporaryVariable;
printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
printf("After swapping, secondNumber = %.2lf", secondNumber);
return 0;
}
3. By Shailja Pandey
Made by Shailja Pandey. Program to swap two numbers without using variable. Source
Enter the first no. 4 Enter the second no. 5 before swap a=4 b=5 after swap a=5 b=4
#include <stdio.h>
int main()
{
int a,b;
printf ("Enter the first no.\n");
scanf("%d",&a);
printf ("Enter the second no.\n");
scanf("%d",&b);
printf("\n before swap a=%d b=%d",a,b);
a = a*b;//10*20=200
b = a/b;//200/20=10
a = a/b;//200/10=20
system("cls");
printf("\n after swap a=%d b=%d",a,b);
return 0;
}
4. By Vijay Madankar
Made by Vijay Madankar. Program to Swap first and last digit of a number. Source
enter any number:123 number=123 swap first and last digit:321
#include <stdio.h>
#include <math.h>
int main() {
int n,swap,first,last,digit;
printf ("enter any number:");
scanf("%d",&n);
last=n%10;
digit=(int)log10(n);
first=(int)(n/pow(10,digit));
swap=last;
swap*=(int)pow(10,digit);
swap+=n%((int)pow(10,digit));
swap-=last;
swap+=first;
printf("\nnumber=%d",n);
printf("\nswap first and last digit:%d",swap);
return 0;
}
5. By Mahnoor Shahzad
Made by Mahnoor Shahzad. Simple Program to swap two numbers. Source
Enter 1st Number: 5 Enter 2nd Number: 6 Before swap are: m= 5 a= 6 After swap are: m= 6 a= 5
#include <stdio.h>
int main() {
int m,a,i;
printf("Enter 1st Number:");
scanf("%d",&m);
printf("Enter 2nd Number:");
scanf("%d",&a);
printf("Before swap are: \n m= %d\n a= %d\n" ,m,a);
i=m;
m=a;
a=i;
printf("After swap are: \n m= %d\n a= %d\n" ,m,a);
return 0;
}
6. By Vivek harsora
Made by Vivek harsora. Number Swap program without any extra variable. Source
6 7 7 6
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d", &a,&b);
a = a + b;
b = a - b;
a = a - b;
printf("%d %d", a, b);
return 0;
}
7. By Vishal Gupta
Made by Vishal Gupta. C Number Swap Program using Bitwise XOR. Source
Enter two integers 8 9 Before swapping i= 8 and k =9 After swapping i= 9 and k = 8
#include <stdio.h>
int main() {
long i, k;
printf("Enter two integers \n");
scanf("%ld %ld", &i, &k);
printf("\n Before swapping i= %ld and k =%ld", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\n After swapping i= %ld and k = %ld", i, k);
return 0;
}
8. By Ganesh
Made by Ganesh. Swap two number using function pointer. Source
Value of i and j before swap is 5 10 Value of i and j after swap is 10 5
#include <stdio.h>
void swap(int *num1,int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 =temp;
}
int main()
{
int i = 5,j=10;
printf("Value of i and j before swap is %d %d\n",i,j);
swap(&i,&j);
printf("Value of i and j after swap is %d %d\n",i,j);
}
9. By Arup Basak
Made by Arup Basak. Source
a=11,b=10
#include <stdio.h>
int main() {
int a,b;
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\n a=%d,b=%d", a , b);
return 0;
}
10. By Alexandros Maskas
Made by Alexandros Maskas. Source
enter 2 numbers to swap 11 12 a is 11 and b is 12 a is 12 and b is 11
#include <stdio.h>
int main()
{
int a,b,temp;
printf("enter 2 numbers to swap \n");
scanf("%d %d",&a,&b);
printf("a is %d and b is %d \n",a,b);
temp=a;
a=b;
b=temp;
printf("a is %d and b is %d \n",a,b);
return 0;
}