111k views
4 votes
Design a function int maxDigit (int num) to find and return the greatest digit from the arguments value num.

Also write a function void result() by passing parameters to print the greatest digit.
please tell in java programming​

Design a function int maxDigit (int num) to find and return the greatest digit from-example-1
User Rhathin
by
4.6k points

1 Answer

6 votes

Answer:

Below is the required JAVA Code:

Step-by-step explanation:

public static int largestDigit(int n) {

if (n < 0) {

return largestDigit(-1 * n);

} else if (n == 0) {

return 0;

} else {

int digit = n % 10;

int maxDigit = largestDigit(n / 10);

if (digit > maxDigit)

maxDigit = digit;

return maxDigit;

}

}

\color{red}Method\;tested\;in\;a\;complete\;java\;program\;if\;you\;are\;interested.

public class LargestDigitRecursive {

public static int largestDigit(int n) {

if (n < 0) {

return largestDigit(-1 * n);

} else if (n == 0) {

return 0;

} else {

int digit = n % 10;

int maxDigit = largestDigit(n / 10);

if (digit > maxDigit)

maxDigit = digit;

return maxDigit;

}

}

public static void main(String[] args) {

System.out.println(largestDigit(14263203));

System.out.println(largestDigit(845));

System.out.println(largestDigit(52649));

System.out.println(largestDigit(3));

System.out.println(largestDigit(0));

System.out.println(largestDigit(-573026));

System.out.println(largestDigit(-2));

}

}

OUTPUT:

6

8

9

3

0

7

2

User Pigeonburger
by
4.4k points