+ 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
4 Respostas
+ 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);
}
}
+ 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);
}
}
+ 2
c++ version
https://code.sololearn.com/cXcds8X0AX0w/?ref=app
+ 1
Thank you all of you :)