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. 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