i got WA in the sum of first and last digit problem, checked the test cases so many times yet i could not find the error. Please help.

Basically in your code, you aren’t saving much of complexity by checking for single digit numbers and also missing the condition for n == 10, anyways, straight forward approach would also do.

My solution got AC, so there is no fault in the test cases. Please re-check your approach.

    Scanner sc = new Scanner(;
	if (sc.hasNext()) {
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			String s = String.valueOf(n);
			int a = Integer.parseInt(s.substring(0, 1));
			int b = Integer.parseInt(s.substring(s.length() - 1));
			System.out.println(a + b);

Oneliner approach could be,

System.out.println(n % 10 + Integer.parseInt(Integer.toString(n).substring(0, 1)));
1 Like

Can’t see your code.

1 Like

Thank you for helping me but i am still getting WA. Tried your code as well, got a WA.

please throw some light.

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
public static void main (String[] args) throws java.lang.Exception
// your code goes hb
Scanner sc=new Scanner(;
int t=sc.nextInt();
int n=sc.nextInt();
int l=n%10;

	   String s=Integer.toString(n);
	   String s1=s.substring(0,1);
	   int f=Integer.parseInt(s1);
	   int sum=l+f;


How come my answer got AC and when the same answer you submitted got WA :thinking:

1 Like

there is surely some test case which i am failing to recognise :pensive:…will check once again

The expected result for the test input:


is 12, not 6.