Using Third variable:-
#include <stdio.h>
#include<conio.h>
void main()
{
int x,y,temp;
printf("Enter the two integers\n");
scanf("%d %d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
}
#include <stdio.h>
#include<conio.h>
void main()
{
int x,y,temp;
printf("Enter the two integers\n");
scanf("%d %d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
}
Without using Third variable:-
#include <stdio.h>
#include<conio.h>
void main()
{
 int x,y;
   printf("Enter two integers:\n");
   scanf("%d%d",&x,&y);
   x = x + y;
   y = x - y;
   x = x - y;
   printf("x = %d\ny= %d\n",x,y);
}
Using Reference
#include <stdio.h>
#include<conio.h>
 void main()
{
   int x, y, *a, *b, temp;
    printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
   a = &x;
   b = &y;
   temp = *b;
   *b   = *a;
   *a   = temp;
 printf("After Swapping\nx = %d\ny = %d\n", x, y);
 getch();
}
 
No comments :
Post a Comment