// this is code to find sum of first & last digit but code chef is showing its wrong
// I have tested the code in compiler no error, works fine , even logic is right why then its not accepting my solution.
cin>>T;
while (T != 0) {
cin>>N;
int sum = 0, digit;
sum = sum + (N % 10);
N = N/10;
while ( N > 0) {
digit = N % 10;
N = N/10;
}
sum = sum + digit;
cout<<sum<<endl;
--T;
}
return 0;
Whenever you think that your code is right but the answer is wrong always dry run your code for the smallest and the largest value according to the constraint.
Now, here what if N is only a single digit value so for that while loop will never run bcoz you are doing N / 10 outside the loop. So the variable digit will never get assign and will have garbage value that’s why your code is giving wrong answer