Write a C program to swap two numbers without using temporary variable.
/*
* Function with return value and no args.
* manipulate() - swaps two numbers without
* temp variable
*/
int manipulate() {
int a, b;
/* get the input values from the user */
printf("Enter your inputs:");
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
/* print the results */
printf("After swapping:\n");
printf("a = %d\nb = %d\n", a, b);
return 1;
}
int main() {
int flag;
printf("Program to swap two numbers without temp variable\n");
flag = manipulate();
if (flag)
printf("Operation success\n");
else
printf("Operation failure\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Program to swap two numbers without temp variable
Enter your inputs:100 200
After swapping:
a = 200
b = 100
Operation success
Program to swap two numbers without temp variable
Enter your inputs:100 200
After swapping:
a = 200
b = 100
Operation success
No comments:
Post a Comment