#include<stdio.h>
#include<conio.h>
void main(){
float a,b;
clrscr();
printf("enter two values\n");
scanf("%f%f",&a&b);
printf("before swapping a=%f and b=%f",a,b);
a=a+b;
b=a-b;
a=a-b;
printf ("after swapping a=%f and b=%f",a,b);
getch();
}
#include<conio.h>
void main(){
float a,b;
clrscr();
printf("enter two values\n");
scanf("%f%f",&a&b);
printf("before swapping a=%f and b=%f",a,b);
a=a+b;
b=a-b;
a=a-b;
printf ("after swapping a=%f and b=%f",a,b);
getch();
}
We first get the sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from sum. There is one problem in this approach, the sum of both numbers may overflow the range of integer, in that case we will get wrong values.
ReplyDeleteC program to swap two numbers without using temporary variable