Which method will return the last digit of a parameter if the parameter is an integer? It should work for negative numbers as well. For example, lastDigit(3572) would return 2 and lastDigit(-947) would return 7
A. public static int lastDigit(int num) {
return (int)Math.floor(num);
}
B. public static int lastDigit(int num) {
return Math.abs(num % 10);
}
C. public static int lastDigit(int num) {
return num / 10;
}
D. public static int lastDigit(int num) {
return num % 10;
}
Answers
Answered by
7
B. public static int lastDigit(int num) {
return Math.abs(num % 10);
}
hope it HELPS
plzzzz mark as BRAINLIEST
Answered by
2
Answer: B
Syntax:
package com.company;
public class method {
public static int lastDigit(int num) {
return Math.abs(num % 10);
}
public static void main(String[] args) {
System.out.println("Positive number last digit: "+lastDigit(3572));
System.out.println("Negative number last digit: "+lastDigit(-947));
}
}
Output:
Positive number last digit: 2
Negative number last digit: 7
You can try to run the syntax on any java software such as IntelliJ IDEA, Netbeans IDE, etc.
Hopefully, my answer could help solve your question and help you can understand better.
Similar questions