Swap Two Numbers Using Third Variable:-
In this c program we swap two numbers by using a third variable. Here we take three int type of variable, name as num1, num2, num3. num1 and num2 variable for store first number and second number likewise and third used for interchanging the value when swapping.
/*
Program : Swap Two Numbers Using Third Variable
Filename : swapThirdVariable.c
IDE Used To Developed : CodeBlocks
Developed By : Pavito Golui
*/
#include<stdio.h>
int main()
{
int num1, num2,num3;
printf("Enter the first number :");
scanf("%d",&num1);
printf("Enter the second number :");
scanf("%d",&num2);
printf("\nBefore swapping first no :%d\n",num1);
printf("Before swapping second no :%d\n\n",num2);
num3=num1;
num1=num2;
num2=num3;
printf("After Swapping First No :%d\n",num1);
printf("After Swapping Second No :%d\n",num2);
return 0;
}
Program : Swap Two Numbers Using Third Variable
Filename : swapThirdVariable.c
IDE Used To Developed : CodeBlocks
Developed By : Pavito Golui
*/
#include<stdio.h>
int main()
{
int num1, num2,num3;
printf("Enter the first number :");
scanf("%d",&num1);
printf("Enter the second number :");
scanf("%d",&num2);
printf("\nBefore swapping first no :%d\n",num1);
printf("Before swapping second no :%d\n\n",num2);
num3=num1;
num1=num2;
num2=num3;
printf("After Swapping First No :%d\n",num1);
printf("After Swapping Second No :%d\n",num2);
return 0;
}
Output :
Enter the first number : 11
Enter the first number : 27
Before swapping first no. : 11
Before swapping second no. : 27
Before swapping first no. : 27
Before swapping second no. : 11