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 digis
.
Here's an example:
Let's say you call sumFirstAndLastDigit(23456);
This should return 8 because 2 + 6 = 8
import java.util.Scanner;
class App {
static int sumFirstAndLastDigits(int number) {
int first = number;
int last = number % 10;
for (first = number; first >= 10; first = first / 10);
return first + last;
}
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Ente number: ");
int number = keyBoard.nextInt();
System.out.println("The sum of the first and Last digis: " + sumFirstAndLastDigits(number));
keyBoard.close();
}
}
Comments
Leave a comment