Define a function int maxDigit(int num)- to find and return the greatest digit from the argument van Also, write a function void result() by passing parameters to print the greatest digit.
Answers
Answered by
4
The given problem is solved using language - Java.
import java.util.Scanner;
public class Program{
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
Program obj=new Program();
System.out.print("Enter a number (0-9): ");
System.out.println("The greatest digit is: "+obj.maxDigit(sc.nextInt()));
}
int maxDigit(int num){
return num==0?0:Math.max(num%10,maxDigit(num/10));
}
}
- The function maxDigit() returns the highest digit of a number using recursion.
- The function is then called inside main() so as to find out the greatest digit present in the number.
- Note: Scanner class is imported so as to take the number as input from the user using nextInt() method.
See the attachment for output.
Attachments:
anindyaadhikari13:
Thanks for the brainliest :)
Similar questions