Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

first and last digit sum java

public class Main {
    public static void main(String[] args) {
        System.out.println(sumFirstAndLastDigit(1221)); 
        // output is 2
    }
    
    public static int sumFirstAndLastDigit (int number) {
        if (number < 0) {
            return -1;
        }
        int lastDigit = number % 10;
        while (number > 9) {
            number /= 10;
        }
        return lastDigit + number;
    }
}
 
PREVIOUS NEXT
Tagged: #digit #sum #java
ADD COMMENT
Topic
Name
3+3 =