Write a C function that takes two integers as arguments and
Write a C function that takes two integers as arguments and swaps two values if the second one is larger. The return type of the function should be void.
Solution
main.c
#include <stdio.h>
/* function declaration */
void swap(int num1, int num2);
int main () {
int x, y;
printf(\"Enter the value of x and y\ \");
scanf(\"%d%d\", &x, &y);
swap(x, y);
return 0;
}
void swap(int num1, int num2) {
int temp;
if(num1<num2)
{
printf(\"Before Swapping\ x = %d\ y = %d\ \",num1,num2);
temp = num1;
num1 = num2;
num2 = temp;
printf(\"After Swapping\ x = %d\ y = %d\ \",num1,num2);
}
else{
printf(\"Before Swapping\ x = %d\ y = %d\ \",num1,num2);
printf(\"No Swapping is done.\ x = %d\ y = %d\ \",num1,num2);
}
}
Output :
