Answer to Question #296373 in Java | JSP | JSF for SandyJay

Question #296373

Write a method called sumFirstAndLastDigist() with one parameter of type int called number.

The methods needs to find the first and Last digist of the parameter number passed to the method, using a loop and return the sum of the first and Last digist


1
Expert's answer
2022-02-11T00:27:39-0500
public class Main {
    static int sumFirstAndLastDigits(int number) {
        int first = 0;
        int last = 0;
        boolean isLast = false;
        while (Math.abs(number) > 0) {
            if (!isLast) {
                last = Math.abs(number % 10);
                isLast = true;
            }
            if (Math.abs(number) < 10) {
                first = Math.abs(number % 10);
            }
            number /= 10;
        }
        return first + last;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS