I found the question in CodeChef which asks us to write a program to calculate the sum of the first and last digit of a number N(1 ≤ N ≤ 1000000) for T test cases(1 ≤ T ≤ 1000)(Here's the full problem definition https://www.codechef.com/problems/FLOW004/). I pass the sample input but when submitting gives me the wrong answer. How should I approach to debug my code. Please refer to the below code which gave the wrong answer, as in how one should identify different kinds of inputs using which one can try to detect the error.
#include <stdio.h>
#include <math.h>
int main(void) {
int t, n, last_term, first_term;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
last_term = n % 10;
while(abs(n) > 10)
{
n = n / 10;
}
first_term = n;
printf("%d\n", abs(first_term) + abs(last_term));
}
return 0;
}
The condition abs(n) > 10
looks wrong.
It should be abs(n) >= 10
or abs(n) > 9
to loop until the number becomes one-digit long.