Check whether a given number is an ugly number or not - GeeksforGeeks

03 Mar, 2021
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++
Java
Python3
C#
Javascript
// 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 notint 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 Codeint main(){    int no = isUgly(14);    if (no == 1)        printf("Yes");    else        printf("No");    return 0;}
// Java implementation to// check if a number is ugly numberclass 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 implementation to check# if a number is an ugly number# or not # Function to check if a number# is an ugly number or notdef 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 Codeif __name__ == "__main__":     no = isUgly(14)         if (no == 1):        print("Yes")    else:        print("No") # This code is contributed by chitranayal
// C# implementation to check// if a number is ugly numberusing System; class GFG{ // Function to check the ugly// numberstatic 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 Codepublic 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
<script> // Javascript implementation to check// if a number is an ugly// number or not // Function to check if a number// is an ugly number or notfunction 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      let no = isUgly(14);    if (no == 1)        document.write("Yes");    else        document.write("No");      // This code is contributed by Mayank Tyagi </script>
No
 
ukasp
Amal Kumar Choubey
mayanktyagi1709
prime-factor
Mathematical
Recursion
School Programming
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Program to multiply two matrices
Fizz Buzz Implementation
Complexity Analysis of Binary Search
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Program for Sum of the digits of a given number
Backtracking | Introduction