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.

#include
using namespace std;

int T = 0;
unsigned int N = 0;

int main() {

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;

}

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

2 Likes

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

3 Likes

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