Ethiopian multiplication: Difference between revisions

Undo revision 113931 by Ledrug (Requirements stipulate halve, double, iseven).
(→‎{{header|C}}: cleanup)
(Undo revision 113931 by Ledrug (Requirements stipulate halve, double, iseven).)
Line 390:
=={{header|C}}==
<lang c>#include <stdio.h>
#include <stdbool.h>
 
int ethiopian(int left, int right)
void halve(int *x) { *x >>= 1; }
void doublit(int *x) { *x <<= 1; }
bool iseven(const int x) { return (x & 1) == 0; }
 
int ethiopian(int leftplier, int right)
int plicand, const bool tutor)
{
int result = 0;
for (; left; left >>= 1, right <<= 1, printf("\n")) {
printf("%4d %6d", left, right);
 
if (tutor)
if (left & 1) result += right;
else printf("ethiopian Xmultiplication of %d by %d\n", plier, plicand);
}
while(plier >= 1) {
printf("-------------\n %5d\n", result);
if ( iseven(plier) ) {
return result;
if (tutor) printf("%4d %6d struck\n", plier, plicand);
} else {
if (tutor) printf("%4d %6d kept\n", plier, plicand);
if (left & 1) result += rightplicand;
}
halve(&plier); doublit(&plicand);
}
return result;
}
 
int main()
{
printf("%d\n", ethiopian(17, 34, true));
return 0;
}</lang>
 
Anonymous user