Advertisement
eranseg

Biggest Digit

Jul 28th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class BiggestDigit {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         int num, bigDigit = 0;
  7.         System.out.println("Please enter a number: ");
  8.         num = sc.nextInt();
  9.         do {
  10.             //Check if the next digit is bigger. Update bigDigit if so.
  11.             bigDigit = bigDigit > num % 10 ? bigDigit : num % 10;
  12.             num /= 10; // Omit the right digit
  13.         } while(num > 0);// As long as there are digits
  14.         System.out.printf("The biggest digit is: %d", bigDigit);
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement