Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Ugly Number problem solution

YASH PAL, 31 July 2024

In this Leetcode Ugly Number problem solution, An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. we have given an integer n, return true if n is an ugly number.

Leetcode Ugly Number problem solution

Problem solution in Python.

class Solution(object):
    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        uglyFactors = [2,3,5]
        if num == 0: return False
        if num == 1: return True
        
        while True:
            if num in uglyFactors:
                return True
            
            factorFound = False
            for factor in uglyFactors:
                if num % factor == 0:
                    factorFound = True
                    num /= factor
                    break
            
            if not factorFound:
                return False

Problem solution in Java.

class Solution {
    public boolean isUgly(int num) {
        int n =num;
        
        while(n>0){
            while(n%2 == 0){
                n= n/2;
            }
            while(n%3 ==0){
                n=n/3;
            }
            while(n%5==0){
                n=n/5;
            }
             if(n == 1){
                 return true;}
            else{
                return false;
            }
        }
        return false;
    }
}

Problem solution in C++.

class Solution {
public:
    bool isUgly(int num) {
        if(num <= 0) return false;
        while(num != 1)
        {
            if(num%5 == 0) num /= 5;
            else if(num%3 == 0) num /= 3;
            else if(num%2 == 0) num /= 2;
            else return false;
        }
        return true;
    }
};

Problem solution in C.

bool isUgly(int num) {
if( num <= 0 ) return false;
if( 1 == num ) return true;
while( num != 1 )
{
    if( num%2 == 0 )
    {
        num = num/2;
    }
    else if( num%3 == 0 )
    {
        num = num/3;
    }
    else if( num%5 == 0 )
    {
        num = num/5;
    }
    else
    {
        return false;
    }
}
return true;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes