Check whether a number is Ugly Number or not
  • uglynumber
  • dynamicprogramming
  •   
  • Posted: 4 years ago
  • Updated: 3 years ago
  • Edit
  • answers (1)
  • views (2182)

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


Posted Answers

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;
}

You need to Sign In to post your solution.