hello im beginner , can u plz answer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

hello im beginner , can u plz answer?

Find the largest digit of a number that are read from the keyword(use Scanner) for example 732, tha program will display 7

23rd Aug 2017, 7:04 PM
Shade96
Shade96 - avatar
4 Answers
+ 2
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String num = sc.nextLine(); int n = 0; for(char ch: num.toCharArray()) { int i = Integer.parseInt(Character.toString(ch)); if(i > n) { n = i; } } System.out.println(n); } } OR: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] num = sc.nextLine().split(""); int n = 0; for(String ch: num) { int i = Integer.parseInt(ch); if(i > n) { n = i; } } System.out.println(n); } }
23rd Aug 2017, 7:28 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = Integer.parseInt(scan.nextLine()); int maxDigit = 0; while (n > 0 && maxDigit != 9) { int digit = n % 10; if (digit > maxDigit) maxDigit = digit; n /= 10; } System.out.println(maxDigit); } }
23rd Aug 2017, 8:25 PM
Boris Batinkov
Boris Batinkov - avatar
23rd Aug 2017, 8:41 PM
Million Gashawbeza
Million Gashawbeza - avatar
+ 1
Thank you all of you :)
24th Aug 2017, 7:27 AM
Shade96
Shade96 - avatar