Write a C program to swap two numbers without using third variable.
/* swap two numbes without using third variable */
void swapTwoNumbers(int num1, int num2) {
printf("\nValues before swapping:\n");
printf("num1: %d num2: %d\n", num1, num2);
num1 = num1 * num2;
num2 = num1 / num2;
num1 = num1 / num2;
printf("\nValues after swapping:\n");
printf("num1: %d num2: %d\n", num1, num2);
return;
}
int main() {
int val1, val2;
/* get the inputs from the user */
printf("Enter your first input:");
scanf("%d", &val1);
printf("Enter your second input:");
scanf("%d", &val2);
/* swap two numbers using functions */
swapTwoNumbers(val1, val2);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your first input:100
Enter your second input:200
Values before swapping:
num1: 100 num2: 200
Values after swapping:
num1: 200 num2: 100
Enter your first input:100
Enter your second input:200
Values before swapping:
num1: 100 num2: 200
Values after swapping:
num1: 200 num2: 100
this link might be help you C program to swap two numbers
ReplyDeletehttp://programmergallery.com/c-program/c-program-swap-two-numbers.php