Please help me in problem

It is showing WA.
here’s the link to the question: here

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
    for(int i=0;i<t;i++){
	    int n,sum,first,last;
	    cin>>n;
	    first=n%10;
	    n=n/10;
	    while(n>0){
	        if(n<10){
	            last=n;
	            n = n/10;
	        }
	        else{
	            n = n/10;
	        }
	    }
	    sum = first + last;
	    cout<<sum<<endl;
	}
	return 0;
}

Here’s how to format your code :slight_smile:

Your code will give the wrong answer if n is less than 10 that is, the last and the first digits are the same. To correct the code, remove “n=n/10” after the line “first=n%10”.

1 Like