- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes

- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages

- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation

- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples

# How To Check Whether a Number is an Ugly Number or Not in Java?

A number is said to be an **ugly number**, if the prime factors of that input number only include 2, 3 and 5.

Maybe some number has some prime factors which have only one factor or two factors but those factors must be one of 2, 3, and 5.

In this article we will see how to check if a number is an** Ugly number **or not by using Java programming language.

## To show you some instances

### Instance-1

Input number is 20.

Let’s check it by using the logic of Ugly number.

Prime factors of 20 = 2, 2, 5

So, as you notice here all the prime factors include either of 2, 3 and 5 only.

Hence, 20 is an ugly number.

### Instance-2

Input number is 30.

Let’s check it by using the logic of Ugly number.

Prime factors of 30 = 2, 3, 5

So, as you notice here all the prime factors include either of 2, 3 and 5 only.

Hence, 30 is an ugly number.

### Instance-3

Input number is 14.

Let’s check it by using the logic of Ugly number.

Prime factors of 14 = 2, 7

So, as you notice here 7 is present at one of the prime factors above.

Hence, 14 is not an ugly number.

## Algorithm

**Step 1**− First we define a function for checking if the input number is a prime number or not.**Step 2**− Collect the input from the user either by static method or by user defined method.**Step 3**− Initialise the loop for finding all the prime factors of input number.**Step 4**− After find the prime factor we run a condition for checking whether the factors have only included 2, 3, and 5 or not.**Step 5**− If the condition is true then we are printing the input number is an ugly number otherwise the input number is not an ugly number.

## Multiple Approaches

We have provided the solution in different approaches.

By Using Static Input Value

By Using User Defined Method

Let’s see the Java program along with its output one by one.

### Approach-1: By Using Static Input Value

In this approach one non−zero, positive integer value will be initialized in the program and then by using the algorithm we can check whether a number is an **ugly number** or not.

#### Example

import java.util.Scanner; public class Main { public static void main(String args[]) { //declare a variable with a static value int inputNumber = 25; //declare a variable with boolean value boolean check = true; //initialise the loop for the checking of ugly number for(int i = 2; i<=inputNumber; i++) { // checks whether numbers only include 2,3 and 5 if(i!=2&&i!=3&&i!=5) { // Checks if there are some other prime factors if(inputNumber%i==0&&checkPrime(i)) { // Sets the flag to false if there are some other prime factors check = false; break; } } } if(check) { System.out.println(inputNumber+" is an ugly number"); } else { System.out.println(inputNumber+" is Not an ugly number"); } } // Function that checks for prime static boolean checkPrime(int number) { boolean flag = true; for(int i = 2; i<=number/2; i++) { if(number%i==0) { flag = false; break; } } return flag; } }

#### Output

25 is an ugly number

### Approach-2: By Using User Defined Method

In this approach one non−zero, positive integer value will be initialized in the program and then we will call a user defined method by passing this input number as parameter.

Inside the method we will check whether a number is an **ugly number** or not by using the algorithm.

#### Example

import java.util.Scanner; public class Main { //initialise main method public static void main(String[] args) { //declare a variable with a static value int inp = 56; //check whether our condition is true or not in user defined method //if true number is ugly otherwise not if(checkUgly(inp)) { System.out.println(inp+" is an ugly number"); } else { System.out.println(inp+" is Not an ugly number"); } } // Function that checks for prime static boolean checkPrime(int number) { boolean flag = true; for(int i = 2; i<=number/2; i++) { if(number%i==0) { flag = false; break; } } return flag; } //define the user defined method //checking for ugly number public static boolean checkUgly(int inputNumber) { //declare a variable with boolean value boolean check = true; //initialise the loop for the checking of Ugly number for(int i = 2; i<=inputNumber; i++) { // checks whether numbers only include 2,3 and 5 if(i!=2&&i!=3&&i!=5) { // Checks if there are some other prime factors if(inputNumber%i==0&&checkPrime(i)) { // Sets the flag to false if there are some other prime factors return false; } } } return true; } }

#### Output

56 is Not an ugly number

In this article, we explored how to check a number whether it is an ugly number or not in Java by using different approaches.

- Related Articles
- How To Check Whether a Number is an Empire Number or Not in Java?
- Program to check a number is ugly number or not in Python
- Check Whether a Number is an Autobiographical Number or Not in JAVA
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- How To Check Whether a Number is Strontio Number or Not in Java?
- How To Check Whether a Number is Tcefrep Number or Not in Java?
- How To Check Whether a Number is Tech Number or Not in Java?
- How To Check Whether a Number Is a Pronic Number or Not in Java?
- How To Check Whether a Number Is a Bouncy Number or Not in Java?
- How To Check Whether a Number is a Evil Number or Not in Java?
- How To Check Whether a Number Is a Fascinating Number or Not in Java?
- How To Check Whether a Number is a Harshad Number or Not in Java?
- How To Check Whether a Number Is a Insolite Number or Not in Java?
- How To Check Whether a Number is a Keith Number or Not in Java?
- How To Check Whether a Number Is a Niven Number or Not in Java?