Ethiopian multiplication: Difference between revisions

Content added Content deleted
m (→‎{{header|ActionScript 2.0}}: Change this header to a works with)
(→‎{{header|C}}: cleanup)
Line 390: Line 390:
=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<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 plier,
int plicand, const bool tutor)
{
{
int result=0;
int result = 0;
for (; left; left >>= 1, right <<= 1, printf("\n")) {
printf("%4d %6d", left, right);


if (left & 1) result += right;
if (tutor)
printf("ethiopian multiplication of %d by %d\n", plier, plicand);
else printf(" X");
}
printf("-------------\n %5d\n", result);
while(plier >= 1) {
return result;
if ( iseven(plier) ) {
if (tutor) printf("%4d %6d struck\n", plier, plicand);
} else {
if (tutor) printf("%4d %6d kept\n", plier, plicand);
result += plicand;
}
halve(&plier); doublit(&plicand);
}
return result;
}
}

int main()
int main()
{
{
printf("%d\n", ethiopian(17, 34, true));
ethiopian(17, 34);
return 0;
return 0;
}</lang>
}</lang>