Swap two numbers without using temporary or
Third Variable in C Language.
Introduction:
In this article i will explain How
to swap/interchange the values of two numbers using C language with an example program. In this
program I’ll not use the third variable to swap numbers.
Implementation: let's create a program to swap the value of two numbers without using third variable.
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b;
clrscr();
printf("Enter
the value of a= ");
scanf("%d",&a);
printf("\nEnter
the value of b= ");
scanf("%d",&b);
printf("\nValue
of a before swapping= %d",a);
printf("\nValue
of b before swapping= %d",b);
a=a+b;
b=a-b;
a=a-b;
printf("\nValue
of a after swapping= %d",a);
printf("\nValue
of b after swapping= %d",b);
getch();
}
Run the program using Ctrl+F9
Output:
Enter the value of a=1
Enter the value of b=2
Value of a before swapping=1
Value of b before swapping=2
Value of a after swapping=2
Value of b after swapping=1
0 comments:
Post a Comment