SWAPPING OF TWO NUMBERS - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Wednesday, October 27, 2010

SWAPPING OF TWO NUMBERS

To write a program to swap the given two values in the function, the values are passed as call by address

Algorithm:
1.. Start the program.
2. Interchange two values using temporary variables
3. The values after swapping are displayed.
4. End the program.

Program:
#include
void swap (int *, int*);
main()
{
int x = 5, y = 10;
clrscr();
printf(“values before swapping are %d %d, x, y);
swap(&x, &y);
printf(”\nThe values after swapping are %d %d”, x,y);
getchO;
}
void swap (int *a, int *b)
{
int t;
t =
*a = *b;
*b = t;
}

Output:
The values before swapping are 5 1 0
The values after swapping are 1 0 5

No comments:

Post a Comment