Hello I am new here can anyone help me find out why is this wrong?

// 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.

using namespace std;

int T = 0;
unsigned int N = 0;

int main() {


while (T != 0) {
    int sum = 0, digit;
    sum = sum + (N % 10);
    N = N/10;
    while ( N > 0) {
        digit = N % 10;
        N = N/10;
    sum = sum + digit;

return 0;


Can you please share the problem link? Also, is your code working for single digits?


mate u have not include header file in ur program eg (#include<bits/stdc++.h>)
check it out :blush:

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


i dont have idea about whether test cases include negative number or not but if yes then its showing wrong answers for negative numbers