### Check whether a number is Ugly Number or not uglynumber dynamicprogramming    Posted: 7 years ago Updated: 6 years ago Edit answers (1) views (3662)

How will you check whether a number is a ugly number or not.

 Posted: 7 years ago Updated: 7 years ago 0 0 Edit If a number has only factors are $$2$$, $$3$$ or $$5$$, it is an ugly number. We can check by continuously dividing it with $$2$$, $$3$$ and $$5$$. If the number finally becomes $$1$$ after divison, it is an ugly number, otherwise not. public boolean IsUgly(int number){ while (number % 2 == 0) number /= 2; while (number % 3 == 0) number /= 3; while (number % 5 == 0) number /= 5; return (number == 1)? true : false; }