Answers for "find sum of first and last digits in java"

0

find sum of first and last digits in java

public class Main {

    public static void main(String[] args) {
        System.out.println(sumFirstAndLastDigit(1221));
        System.out.println(sumFirstAndLastDigit(2342));
        System.out.println(sumFirstAndLastDigit(10));
        System.out.println(sumFirstAndLastDigit(-123));
	// write your code here
    }
    public static int sumFirstAndLastDigit(int number){
        if (number<0){
            return -1;  //checking boundary conditions
        }
        else if(number==0){
            return 0;
        }
        else{
            int lastDigit=number%10; // to extract last significant digit
            while(number>=10){
                number=number/10;
            }
            int firstDigit=number;
            return (lastDigit+firstDigit);
        }
    }
Posted by: Guest on September-16-2020

Code answers related to "find sum of first and last digits in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language