Please explain me,what wrong is with my code:
Code:
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{ unsigned long long n;
cin>>n;
int firstdigit;
int lastdigit=n%10;
while(n>=10)
{
n%10;
n=n/10;
firstdigit=n;
}
cout<<firstdigit+lastdigit<<endl;
}
return 0;
}
So,as you can see I have tried the problem with the given three test cases along with additional 5 inputs
and getting the desired output,but still my code is not getting accepted.Can anyone explain,why so?
do the above change and rest is all right
your output is wrong for single digit no:
ex-7 then in this case according to yur code
last digit is 7 is correct but first digit has no value since you have made while loop for>=10
Thank you!I got it now actually my solution was not working for single digit number as pointed out by the below user. i didn’t actually set ‘firstdigit=n’ which resulted in not working for summation of the single digit number(input:7 output: sum=14,but I got the sum as ‘7’ instead.