I'm looking for a way to multiply the digits of two numbers (not necessarily of the same digits length) recursively without using loops in the following way: let's say the numbers are 123 and 567. I'm trying to figure out a way to print:
5
6
7
10
12
14
15
18
21
it's the left most digit of the first number times every digit of the second number starting from the left and moving in both the right most.
the function must fit the prototype:
void multi(int a, int b);
I've managed to dive recursively to 1 and 5 from there to 1 56 and then 1 567 and in every call I print the result of a%10 * b%10. but when backtracking to 12 567 the function dives to 1 567 again.
here's my best attempt:
int main()
{
int a, b;
scanf("%d %d", &a, &b);
multi(a, b);
return 0;
}
void multi(int a, int b)
{
if (a == 0)
return;
multi(a / 10, b);
if(b /10 != 0)
multi(a, b / 10);
printf("%d\n", a % 10 * b % 10);
}
list of restricions:
no loops
single function
mandatory prototype