Swapping of two numbers(integers)-by enggfact - Enggfact

Latest

Convert your Passion into your Profession

Saturday, August 31, 2019

Swapping of two numbers(integers)-by enggfact

There are two types of swapping-
1-By using third variable
2-Without using third variable

1)By using third variable-

#include<stdio.h>
int main()
{
int x,y,temp;//variables
printf("ENTER TWO NUMBERS");
scanf("%d %d",&x,&y);
printf("\n BEFORE SWAPPING %d %d",x,y);
temp=x;
x=y;
y=temp;
printf("\n AFTER SWAPPING %d %d",x,y);
return 0;
}

2)Without using third variable-

#include<stdio.h>
int main()
{
int x,y;//variables
printf("ENTER TWO NUMBERS");
scanf("%d %d",&x,&y);
printf("\n BEFORE SWAPPING %d %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\n AFTER SWAPPING %d %d",x,y);
return 0;
}

For more C programs please subscribe our blog- https://enggfact.blogspot.com





No comments:

Post a Comment