• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
December 05, 2022 |2.0K Views

Javascript program to check if a given number is Ugly Number or not

Description
Discussion

In this video, we will see write a Javascript program to Check if a Given Number is an Ugly Number or not. 

What are Ugly numbers?
The ugly numbers are numbers whose only prime factors are 2, 3, or 5. The first 11 ugly numbers are 1,2,3,4,5,6,8,9,10,12,15.

For Examples:

Input: Number = 14 
Output: No 
Explanation:  14 is not ugly since it includes another prime factor 7.

Input: Number = 6 
Output: Yes 
Explanation: 6 is ugly since it includes 2 and 3

Algorithm:
Step 1: First we iterate for all positive integers till the ugly number count is smaller than n.
Step 2:If an integer is ugly then increment the ugly number count.
Step 3: To check if a number is ugly, divide the number by the greatest divisible powers of 2, 3, and 5.
Step 4: if the number becomes 1 then it is an ugly number otherwise not.

Approaches to check if a number is Ugly or not

1. Using Loop
2. Using recursion