-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.c
37 lines (31 loc) · 1000 Bytes
/
swap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
// function to swap two numbers using call by value
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
// function to swap two numbers using call by reference
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// using call by value method
printf("\nBefore swapping (call by value):\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
swapByValue(num1, num2);
printf("After swapping (call by value):\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
// using call by reference method
printf("\nBefore swapping (call by reference):\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
swapByReference(&num1, &num2);
printf("After swapping (call by reference):\n");
printf("num1 = %d, num2 = %d\n", num1, num2);
return 0;
}