Sunday, May 8, 2011

Swapping without using temporary variable in C - Multiplication & Division

/**
* Swapping without using a temporary variable in C
* (Multiplication & Division)
**/
#include<stdio.h>
int main(void)
{
int a = 5, b = 10;
printf("Initially, a = %d and b = %d\n", a, b);
a = a * b; /* a = 5 * 10 = 50 */
b = a / b; /* b = 50 / 10 = 5 */
a = a / b; /* a = 50 / 5 = 10 */
printf("After swapping, a = %d and b = %d\n", a, b);
return 0;
}
view raw swap_mul_div.c hosted with ❤ by GitHub
Written by

No comments:

Post a Comment