• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 29, 2022 |29.1K Views
C++ Program to Check if a Given Number is Ugly Number or not
Description
Discussion

In this video, we will see C++ Program to Check if a Given Number is an Ugly Number or not

What are Ugly Numbers?
The ugly numbers are numbers whose only prime factors are 2, 3, or 5.

Approach used:
Here we use a while loop with if-else conditions.

Algorithm:
Step 1: First we iterate for all positive integers till the ugly number count is smaller than n.
Step 2: If an integer is ugly then increment the ugly number count.
Step 3: To check if a number is ugly, divide the number by the greatest divisible powers of 2, 3, and 5.
Step 4: If the number becomes 1 then it is an ugly number otherwise not.

For Example:

Example 1:
Input: 
Number = 14
Output: No
Explanation: 14 is not ugly since it includes another prime factor 7.

Example 2:
Input: Number = 6
Output: Yes
Explanation: 6 is ugly since it includes 2 and 3

Ugly Numbers:
https://www.geeksforgeeks.org/ugly-numbers/ 

Check if a number is ugly or not:
https://www.geeksforgeeks.org/check-whether-a-given-number-is-an-ugly-number-or-not/

Read More