find sum of first and last digits in java

Solutions on MaxInterview for find sum of first and last digits in java by the best coders in the world

showing results for - "find sum of first and last digits in java"
Georgina
18 Sep 2019
1
2public class Main {
3
4    public static void main(String[] args) {
5        System.out.println(sumFirstAndLastDigit(1221));
6        System.out.println(sumFirstAndLastDigit(2342));
7        System.out.println(sumFirstAndLastDigit(10));
8        System.out.println(sumFirstAndLastDigit(-123));
9	// write your code here
10    }
11    public static int sumFirstAndLastDigit(int number){
12        if (number<0){
13            return -1;  //checking boundary conditions
14        }
15        else if(number==0){
16            return 0;
17        }
18        else{
19            int lastDigit=number%10; // to extract last significant digit
20            while(number>=10){
21                number=number/10;
22            }
23            int firstDigit=number;
24            return (lastDigit+firstDigit);
25        }
26    }