Write a C Program To Swap Two Numbers Using Functions
Answers
Swap function in C language
void swap(int*, int*); //Swap function declaration.
printf("Enter the value of x and y\n"); scanf("%d%d",&x,&y);
return 0; } //Swap function definition. void swap(int *a, int *b) { int t;
C Program and the output for the above question is stated below:
Output :
If the user input as 8 and 4, then the output is 4 and 8.
If the user input as 9 and 6, then the output is 6 and 9.
Explanation:
#include <stdio.h>//Header file.
void Swap(int first_number,int Second_number)//Swapping function which is used to swap the number.
{
first_number=first_number+Second_number-(Second_number=first_number);//Swaping operation.
printf("The first number is %d and the second number is %d after swapping",first_number,Second_number);//Print the number after swaping.
}
int main()//Main function.
{
int first_number,Second_number;//Variable declaration.
printf("Enter the two number for swapping: ");//Print the message for the input.
scanf("%d %d",&first_number,&Second_number);//Take the input.
Swap(first_number,Second_number);//Pass the input value to the function.
return 0;
}
Code Explanation :
- The above code is in C-language, in which the first line is used to instruct the user, then the second line is used to take the input and the third line is used to pass the value to the user-defined function for Swapping.
- Then the swap function swap the number with the help of some operation, then the swapping value is printed on the screen.
Learn More ;
- C-Program : https://brainly.in/question/3999878