Thursday, October 30, 2008

swap2

Algorithm

Step 1: Begin

Step 2: Read a, b

Step 3: t = a

Step 4: a = b

Step 5: b = t

Step 6: Print a, b

Step 7: End


Flowchart


Program

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

Illustration of the program:

Variables a, b are used to store two integer values and t is temporary variable.

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

the statements,

t = a, t = 4

a = b, a = 6

b = t, b = 4

So, the output is

After swapping A = 6, B = 4


index

No comments: