Skip to content

Instantly share code, notes, and snippets.

@jacobsmith
Created December 28, 2013 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobsmith/8165201 to your computer and use it in GitHub Desktop.
Save jacobsmith/8165201 to your computer and use it in GitHub Desktop.
/* multiplication_a_la_russe.c
* multiplication a la russé
*/
#include <stdio.h>
int main()
{
int op1, op2, total_sum = 0;
printf("Enter two integers: ");
scanf("%d %d", &op1, &op2);
printf("-------------------\n");
while (op1 >= 1) {
printf("%d \t %d \t", op1, op2);
if ( op1 % 2 == 0 ) {
printf("---\n");
} else {
printf("%d \n", op2);
total_sum += op2;
}
// halve op1, double op2
op1 >>= 1;
op2 <<= 1;
}
printf("Final Product: %d \n", total_sum);
return 0;
}
@jacobsmith
Copy link
Author

More information on the algorithm here: http://mathworld.wolfram.com/RussianMultiplication.html

@nwmcsween
Copy link

  • c int main(void) non void fn's are implicitly int.
  • c if (op1 % 2 == 0)... can be c if (!(op1 % 2)) as anything not zero is true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment