Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • 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
Programmingoneonone

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 solutions

Post navigation

Previous post
Next post

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
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes