Java Program to Check Ugly Number

In the previous article, we have discussed Java Program to Check Pronic 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.

Program to Check Ugly Number

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.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

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:Java Program to Check Ugly Number 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:Java Program to Check Ugly Number 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.println("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

Method-3: Java Program to Check Ugly Number By Using User Defined Method

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.println("Enter a number : ");
        int num = scan.nextInt();
        //calling the user defined method
        //to check Ugly number or not.
        checkNumber(num);
    }

    //checkNumber() method to check ugly number
    public static void checkNumber(int num)
    {

        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

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: