codechef program #9: First and Last Digit.

PROBLEM STATEMENT
If Give an integer N . Write a program to obtain the sum of the first and last digit of this number.
Input
The first line contains an integer T, total number of test cases. Then follow T lines, each line contains an integer N.
Output
Display the sum of first and last digit of N.
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 1000000
Example
Input 3 1234 124894 242323 Output 5 5 5
My Solution:
import java.util.*;import java.io.*;class Main{public static void main(String[] args){try{boolean l=true;int i=0,m=55,n,b=0;Scanner input = new Scanner(System.in);BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));int q=input.nextInt();int arr1[]=new int[q];while (i<q){n=input.nextInt();while(n>0){m=n%10;n=n/10;if (l)b=b+m;l=false;}b=b+m;arr1[i]=b;b=0;l=true;i++;}for (int k=0;k<arr1.length;k++){System.out.println(arr1[k]);}}catch(Exception e){System.out.println(e);}}}
posted by lol ik
Comments
Post a Comment