Java Interview Program - Check Whether a Number is an Ugly Number

Check Whether a Number is an Ugly Number

An ugly number is a positive number whose prime factors are limited to 2, 3, and 5. This means when you divide the number repeatedly by 2, 3, and 5, you should eventually reach 1. For example, 6 is an ugly number because it can be factored as 2 × 3.

This concept is commonly asked in interviews to test your understanding of loops, basic arithmetic, and number theory. In this program, you’ll write logic to check whether a number is ugly or not.

Examples

  • Input: 6
    Output: Ugly Number
    Explanation: 6 → 3 → 1 (dividing by 3 then 2)
  • Input: 14
    Output: Not an Ugly Number
    Explanation: 14 is divisible by 2 and 7 → 7 is not allowed
  • Input: 1
    Output: Ugly Number
    Explanation: By convention, 1 is considered an ugly number
  • Input: 0
    Output: Not an Ugly Number
    Explanation: Ugly numbers are positive integers only

Interviewer Expectations

The interviewer wants to assess:

  • Your ability to break down a number using prime factors
  • Your loop logic and conditional handling
  • Your understanding of edge cases (like 0 and 1)
  • Code clarity and variable naming

Approach

We repeatedly divide the number by 2, 3, and 5 until it's no longer divisible by any of them. If the resulting number becomes 1, it's an ugly number. Otherwise, it contains other prime factors and is not ugly.

Dry Run with Example: Input = 30

  1. 30 ÷ 2 = 15
  2. 15 ÷ 3 = 5
  3. 5 ÷ 5 = 1
  4. Final value is 1 → Ugly Number

Java Program

public class UglyNumberCheck {
    public static void main(String[] args) {
        int num = 30; // You can change this value to test other numbers

        if (isUglyNumber(num)) {
            System.out.println(num + " is an Ugly Number");
        } else {
            System.out.println(num + " is NOT an Ugly Number");
        }
    }

    public static boolean isUglyNumber(int num) {
        if (num <= 0) return false;

        for (int factor : new int[]{2, 3, 5}) {
            while (num % factor == 0) {
                num /= factor;
            }
        }

        return num == 1;
    }
}
30 is an Ugly Number

Possible Followup Questions with Answers

Q1: How do you generate the first N ugly numbers?

You can use dynamic programming or a priority queue to generate the first N ugly numbers by multiplying previous ugly numbers with 2, 3, and 5, and tracking duplicates.

Q2: Why is 1 considered an ugly number?

By convention, 1 is included because it doesn't have any prime factors outside 2, 3, and 5.

Q3: How do you modify this to allow more primes?

You can add more allowed primes to the factor array and apply the same division logic.

int[] allowedPrimes = {2, 3, 5, 7};

Q4: Can this be done recursively?

Yes, but it's less efficient due to repeated computations. Iterative methods are preferred for performance.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...