Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170629;
- import java.util.Scanner;
- public class LoopsEx03Slide64 {
- public static void main(String[] args) {
- // input: integer number. example: 512624
- // output: maximum digit in the number. example: 6
- // create a scanner
- Scanner s = new Scanner(System.in);
- // ask for input
- System.out.print("Enter an integer number: ");
- // get and save input value
- int n = s.nextInt();
- // close scanner
- s.close();
- int maxDigit = n % 10; // calculated: maximum digit so far
- int tmp = n / 10; // calculated: number with digits to be checked
- // while there are more digits to check
- while (tmp > 0) {
- // calculate most right digit
- int digit = tmp % 10; // calculated: current digit to be checked
- // if even digit then
- if (digit > maxDigit) {
- // set it to be the maximum
- maxDigit = digit;
- }
- tmp /= 10; // drop this digit and continue to check the others
- }
- // display output on the screen
- System.out.printf("n=%d , maximum digit=%d\n", n, maxDigit);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement