Skip to content

Instantly share code, notes, and snippets.

@mbalayil
Created May 8, 2011 18:48
Show Gist options
  • Save mbalayil/961589 to your computer and use it in GitHub Desktop.
Save mbalayil/961589 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment