Given an integer N, the task is to find out whether the given number is an Ugly number or not .
Ugly numbers are numbers whose only prime factors are 2, 3 or 5.
Examples:
Input: N = 14
Output: No
Explanation:
14 is not ugly since it includes another prime factor 7.Input: N = 6
Output: Yes
Explanation:
6 is a ugly since it includes 2 and 3.
Approach: The idea is to use recursion to solve this problem and check if a number is divisible by 2, 3 or 5. If yes then divide the number by that and recursively check that a number is an ugly number or not. If at any time, there is no such divisor, then return false, else true.
Below is the implementation of the above approach:
C++
// C++ implementation to check // if a number is an ugly // number or not #include <stdio.h> #include <stdlib.h> // Function to check if a number // is an ugly number or not int isUgly(int n) { // Base Cases if (n == 1) return 1; if (n <= 0) return 0; // Condition to check if the // number is divided by 2, 3, or 5 if (n % 2 == 0) { return (isUgly(n / 2)); } if (n % 3 == 0) { return (isUgly(n / 3)); } if (n % 5 == 0) { return (isUgly(n / 5)); } // Otherwise return false return 0; } // Driver Code int main() { int no = isUgly(14); if (no == 1) printf("Yes"); else printf("No"); return 0; } |
Java
// Java implementation to // check if a number is ugly number class GFG { // Function to check the ugly // number static int isUgly(int n) { // Base Cases if (n == 1) return 1; if (n <= 0) return 0; // Condition to check if // a number is divide by // 2, 3, or 5 if (n % 2 == 0) { return (isUgly(n / 2)); } if (n % 3 == 0) { return (isUgly(n / 3)); } if (n % 5 == 0) { return (isUgly(n / 5)); } return 0; } // Driver Code public static void main(String args[]) { int no = isUgly(14); if (no == 1) System.out.println("Yes"); else System.out.println("No"); } } |
Python3
# Python3 implementation to check # if a number is an ugly number # or not # Function to check if a number # is an ugly number or not def isUgly(n): # Base Cases if (n == 1): return 1 if (n <= 0): return 0 # Condition to check if the # number is divided by 2, 3, or 5 if (n % 2 == 0): return (isUgly(n // 2)) if (n % 3 == 0): return (isUgly(n // 3)) if (n % 5 == 0): return (isUgly(n // 5)) # Otherwise return false return 0 # Driver Code if __name__ == "__main__": no = isUgly(14) if (no == 1): print("Yes") else: print("No") # This code is contributed by chitranayal |
C#
// C# implementation to check // if a number is ugly number using System; class GFG{ // Function to check the ugly // number static int isUgly(int n) { // Base Cases if (n == 1) return 1; if (n <= 0) return 0; // Condition to check if // a number is divide by // 2, 3, or 5 if (n % 2 == 0) { return (isUgly(n / 2)); } if (n % 3 == 0) { return (isUgly(n / 3)); } if (n % 5 == 0) { return (isUgly(n / 5)); } return 0; } // Driver Code public static void Main(String []args) { int no = isUgly(14); if (no == 1) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by amal kumar choubey |
No
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Super Ugly Number (Number whose prime factors are in given set)
- Ugly Numbers
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Program to check whether the given number is Buzz Number or not
- Check whether the given number is Euclid Number or not
- Check whether given number N is a Moran Number or not
- Check whether a given number N is a Nude Number or not
- Check whether a given number is Polydivisible or Not
- Check whether a given Number is Power-Isolated or not
- Check whether the given number is Wagstaff prime or not
- Check whether a number has consecutive 0's in the given base or not
- Check whether the binary equivalent of a number ends with given string or not
- Check whether a given point lies inside a triangle or not
- Check whether triangle is valid or not if sides are given
- Check whether the given numbers are Cousin prime or not
- Check whether two strings are equivalent or not according to given condition
- Check whether the triangle is valid or not if angles are given
- Check whether Quadrilateral is valid or not if angles are given
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check whether triangle is valid or not if three points are given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

