Created
December 28, 2013 22:48
-
-
Save jacobsmith/8165201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} |
c int main(void)
non void fn's are implicitly int.c if (op1 % 2 == 0)...
can bec 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
More information on the algorithm here: http://mathworld.wolfram.com/RussianMultiplication.html