0
The question I need to solve is the following: The person gives a number, I need to multiply by 2 all the numbers that are in the "index" even, the problem is that the index starts at 1 on the right. For example, the number 49927398716 would be rewritten as 4(18)9(4)7(6)9(16)7(2)6.
My reasoning was: invert the number so that I can multiply the indices [i] + 1 * 2. I was able to invert, however, when I multiply the numbers, the program returns letters instead of numbers.
Code:
void checkfinal(char num[]){
int tamanho = strlen(num);
char aux[50];
int i, j=0;
for(i= tamanho-1;i>=0;i--){
aux[j]=num[i];
j++;
}
aux[j]=0;
for( j = 1; j <= tamanho; j++)
{
if(j%2==0){
aux[j-1] = aux[j-1] * 2;
}
}
printf("%s", aux);
}
The problem is very ill-defined so you can interpret it in several ways and the question does not have a specific question. So I think there are several errors in the algorithm. and probably the solution shouldn’t even be this.
– Maniero