Monday, June 18, 2012

To swap two numbers without using a third variable


/* To swap two numbers without using a third variable */

#include<stdio.h>

#include<conio.h>

void main()

{

 int a,b;

 clrscr();

 printf("Enter the First Number:\n");

 scanf("%d",&a);

 printf("Enter the Second Number:\n");

 scanf("%d",&b);

 printf("\nResult Before Swapping");

 printf("\nA==%d\nB==%d",a,b);

/* Swapping without using third variable */

 a=a+b;

 b=a-b;

 a=a-b;

 printf("\n\nResult after Swapping");

 printf("\nA==%d\nB==%d",a,b);

 getch();

}

 

OUTPUT

Enter the First Number:

43

Enter the Second Number:

67

 

Result Before Swapping

A==43

B==67

 

Result after Swapping

A==67

B==43

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.