C programming question (swap two numbers)


#1

write a c program to swap two numbers.
requirements:-
1.use one function to swap numbers.
2.read two inputs
3.using pass by reference method pass the addersses of these two inputs to function
4.fuction actual arguments are two pionters
5.using these two pointers perform addition ans substraction only to swap these numbers


#2

void swap(int* a,int *b)
{
*b=*a-*b;
*a=*a-*b;
*b=*a+*b;
}


#3

#include<stdio.h>
void swap(int*,int*);
void main()
{
int x,y;
printf(“enter two numbers”);
scanff("%d %d",&x,&y);
swap(&x,&y);
printf(“after swaping values of x and y is”);
printf("%d %d",x,y);
}
void swap(int* a,int *b)
{
*b=*a+*b;
*a=*b-*a;
*b=*b-*a;
}


#4

#include <stdio.h>
int main()
{
double firstNumber, secondNumber;

printf("Enter first number: ");
scanf("%lf", &firstNumber);

printf("Enter second number: ");
scanf("%lf",&secondNumber);



firstNumber = firstNumber - secondNumber;
secondNumber = firstNumber + secondNumber;
firstNumber = secondNumber - firstNumber;

printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
printf("After swapping, secondNumber = %.2lf", secondNumber);

return 0;

}


#5

#include<stdio.h>
#include<stdlib.h>
void swap(int*,int*);
int main()
{
int p, q;
printf(“enter two numbers”);
scanf("%d %d",&p,&q);
swap(&p, &q);
printf(“after swapping”\n);
printf(“p=%d\nq= %d\n”,p, q);
}
void swap(int* x, int *y)
{
*y=*x+*y;
*x=*y-*x;
*y=*y-*x;
}