CodeChef - First and Last Digit

First and Last Digit problem from CodeChef.

CodeChef - First and Last Digit

Problem

Given an integer N . Write a program to obtain the sum of the first and last digits of this number.

Input Format

The first line contains an integer T, the total number of test cases. Then follow T lines, each line contains an integer N.

Output Format

For each test case, display the sum of first and last digits of N in a new line.

Constraints

  • 1 <= T <= 1000
  • 1 <= N <= 1000000

Solution in Java:

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	  Scanner sc = new Scanner(System.in);
	  
	  int T = sc.nextInt();
	  
	  while( T-- > 0){
	      int l = sc.nextInt();
	      int sum = l % 10;
	      int rem = 0;
	      while(l != 0) {
	          rem = l % 10;
	          l = l/ 10;
	      }
	      sum += rem;
	      System.out.println(sum);
	  }
	}
}