Thursday, October 30, 2008

swap1

Algorithm

Step 1: Begin

Step 2: Read a, b

Step 3: a = a + b

Step 4: b = a - b

Step 5: a = a - b

Step 6: Print a, b

Step 7: End


Flowchart



Program

/*swapping two numbres*/
#include
#include
void main()
{
int a,b;
clrscr();
printf ("Enter two values");
scanf("%d%d",&a,&b);
printf ("Before swapping A=%d, B=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf ("After swapping A=%d, B=%d", a, b);
}

Illustration of the program:

Variables a, b are used to store two integer values.

If you entered 4 6, 4 is assigned to a and 6 is assigned to b.

the statements,

a = a + b, a = 4 + 6 = 10

b = a - b, b = 10 - 4 = 6

a = a - b, a = 10 - 6 = 4

So, the output is

After swapping A = 6, B = 4


index

No comments: