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

Question #296566

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

1
Expert's answer
2022-02-11T11:56:58-0500


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();
	}
}

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