Hello looking for help again.
Ok some update. I managed to put my codes together, but something is still wrong because every number I checked returned invalid. I'd like to know if my "checker" code was ok or did I miss something?
int a = 0;
int sum = 0;
for (int i = n - 2; i >= 0; i = i - 2)
{
int digit = cn[i] - 48;
int times2 = 2 * digit;
sum += times2 / 10 + times2 % 10;
}
int sum2 = 0;
for (int i = n - 1; i >= 0; i = i - 2)
{
int digit = cn[i] - 48;
sum2 += digit;
}
int sum3 = sum + sum2;
if (sum3 / 10 == 0)
{
a = 0;
}
else
{
a = 1;
}
At first I wanted to use bool type for this checker, but error message popped up when I tried to compile. Don't know why. So instead of bool type, I assigned an integer at the beginning and did the following instead. Is this practical? and why didn't bool type work?
if (a == 1)
{
printf("INVALID\n");
}
Please give me some pointers. Thank you!
get_long_long
to get the number as along long
. Besides that, getting the digits with/
and%
is a good idea. Try writing out on a piece of paper a long number, and use these two operators to get the digits (starting from the ones place). You can make a loop from that.