C languages Write a swap function with the following functio

C languages

Write a swap() function with the following function header:

void swap(int *p1, int *p2);

Use this function to swap the values of two integers.

Solution

#include <stdio.h>

//swap function
void swap(int *p1,int *p2){
int temp; //third variable
temp=*p1; //value of p1 stored in temp
*p1=*p2; //value of p2 stored in p1
*p2=temp; //value of temp i.e p1 stored in p2 ,hence values are swapped
}
//end of swap function
int main()
{int x=5,y=10;


swap(&x,&y); //calling swap function
printf(\"x= %d \",x);
printf(\"y= %d \",y);

return 0;
}

********OUTPUT*******

x= 10 y= 5

********OUTPUT*******

C languages Write a swap() function with the following function header: void swap(int *p1, int *p2); Use this function to swap the values of two integers.Soluti

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site