C program to swap value of two variables without using third variable.

#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();
}

1 comment:

  1. 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.
    C program to swap two numbers without using temporary variable

    ReplyDelete