logo Practice-It logo

BJP5 Exercise 5.19: firstDigit

Language/Type: Java method basics mod
Author: Will Beebe (on 2019/09/19)

Write a method named firstDigit that returns the first digit of an integer. For example, firstDigit(3572) should return 3. It should work for negative numbers as well. For example, firstDigit(-947) should return 9.

Call Value Returned
firstDigit(3572) 3
firstDigit(-947) 9
firstDigit(6) 6
firstDigit(35) 3
firstDigit(123456) 1

(Hint: Use a while loop. You may not use a String to solve this problem.)

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.