Write a C-program Swap Two Number using Pointer and Function:
Swap Two Number using Pointer and Function:
Tips:
1.Take three variables.
2.Take two inputs from user.
3.Assign variables one by one each other.
4.Take pointer variable and address to swap.
4.Take pointer variable and address to swap.
Code:
void add(int *,int *);
void main()
{
int a,b;
printf("Enter two number two swap:\n");
scanf("%d%d",&a,&b);
printf("\nyou decide:\n a=%d and b=%d",a,b);
add(&a,&b);
printf("\nThe swap number is: a=%d and b=%d",a,b);
getch();
}
void add(int *x, int *y)
{
int s;
s=*x;
*x=*y;
*y=s;
}
void main()
{
int a,b;
printf("Enter two number two swap:\n");
scanf("%d%d",&a,&b);
printf("\nyou decide:\n a=%d and b=%d",a,b);
add(&a,&b);
printf("\nThe swap number is: a=%d and b=%d",a,b);
getch();
}
void add(int *x, int *y)
{
int s;
s=*x;
*x=*y;
*y=s;
}
Output:
Please comment!
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)