Java Program to Check Whether a Given Number Is Ugly Number or Not

You are currently viewing Java Program to Check Whether a Given Number Is Ugly Number or Not

Java program to check whether a given number is ugly number or not

import java.util.Scanner;

public class CheckUglyNumbers
{
	public static void main(String[] args)
	{
		// create object of scanner class.
		Scanner Sc = new Scanner(System.in);

		// enter the positive number
		System.out.print("Enter the number : ");
		int n = Sc.nextInt();  		
		if (n <= 0)
		{
			System.out.print("Enter correct/+ve number.");
		}
		int x = 0;
		while (n != 1)
		{
			// loop to obtain the result.
			if (n % 5 == 0)
			{
				n /= 5;
			}

			else if (n % 3 == 0) 
			{
				n /= 3;
			}

			else if (n % 2 == 0)
			{
				n /= 2;
			}

			else
			{
				System.out.print("The given number is not a ugly number.");
				x = 1;
				break;
			}
		}
		if (x==0)
			System.out.print("The given number is an ugly number.");
		System.out.print("\n");
	}
}

Output

First run:
Enter the number : 42
The given number is not a ugly number.

Second run:
Enter the number : 120
The given number is an ugly number.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply