Java Program to Check Ugly Number

Program to Check Ugly Number

In this article we are going to understand what Ugly number is and how we can check whether a number is Ugly or not in Java with examples.

Ugly numbers are numbers whose prime factors only contain 2,3 or 5.

Example:

100 = 2*2*5*5 is Ugly Number
52 = 2*2*13 is not Ugly Number
72= 2*2*2*3*3 is Ugly number

In the above examples the numbers 100 and 72 are  Ugly numbers as their prime factors only contain 2,3 or 5. And 52 is not a Ugly number as it contains 13 in its prime factor.

Let’s see different ways to check ugly number.

Approach :

  1. Enter/declare a number which we store in a integer variable.
  2. We then use a while loop to iterate until the number is not equal to 1,
  3. We check if the number is divisible by 2,3, or 5. If it is then we divide the number by it. If not flag is set to false and it breaks out of the loop
  4. If flag is true then the number is said to  be an ugly number else not.

Method-1: By Using Static Value

import java.util.Scanner;
public class UglyNumber
{
    public static void main(String args[])
    {
        //A number declared
        int num = 72;

        boolean flag = true;
        int temp = num;
        // Checks whether the number is divisible  y 2,3 or 5
        // If diviisible, it divides the number by it and iterates until number is one
        while(temp!=1)
        {
            if(temp%2==0)
                temp=temp/2;
            else if(temp%3==0)
                temp=temp/3;
            else if(temp%5==0)
                temp=temp/5;
            else{
                flag = false;
                break;
}
        }
        if(flag)
        {
            System.out.println(num+" is an Ugly Number");
        }
        else
        {
            System.out.println(num+" is Not an Ugly Number");
        }
    }
}
Output:

72 is an Ugly Number

Method-2: By User Input Value

import java.util.Scanner;
public class UglyNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        boolean flag = true;
        int temp = num;
        // Checks whether the number is divisible  y 2,3 or 5
        // If diviisible, it divides the number by it and iterates until number is one
        while(temp!=1)
        {
            if(temp%2==0)
                temp=temp/2;
            else if(temp%3==0)
                temp=temp/3;
            else if(temp%5==0)
                temp=temp/5;
            else{
                flag = false;
                break;
}
        }
        if(flag)
        {
            System.out.println(num+" is an Ugly Number");
        }
        else
        {
            System.out.println(num+" is Not an Ugly Number");
        }
    }
}
Output:

Case-1

Enter a number : 72
72 is an Ugly Number

Case-2

Enter a number : 56
56 is an Ugly Number

Leave a Comment