# Ugly Number Program in Java

## A number is said to be an Ugly number if positive numbers whose prime factors only include 2, 3, 5.

For example, 6(2×3), 8(2x2x2), 15(3×5) are ugly numbers while 14(2×7) is not ugly since it includes another prime factor 7. Note that 1 is typically treated as an ugly number.

``````import java.util.Scanner;

public class UglyNumber
{
public static void main(String[] args)
{
int n;
boolean flag=true;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
while (n != 1)
{
if (n % 5 == 0)
{
n /= 5;
}
else if (n % 3 == 0)
{
n /= 3;
}
else if (n % 2 == 0)
{
n /= 2;
}
else
{
flag=false;
break;
}
}
if (flag)
{
System.out.println("Ugly number.");
}
else
{
System.out.println("Not Ugly number.");
}
}
}
``````

#### Output:

```Enter number=15
Ugly Number```
##### What is Ugly Number?
A number is said to be an Ugly number if positive numbers whose prime factors only include 2, 3, 5.

##### What is Ugly Number in Java?
A number is said to be an Ugly number if positive numbers whose prime factors only include 2, 3, 5.

java java tutorials learn java study java
Recent Posts