8

C++ Program to Check if a Given Number is Ugly Number or not

 1 year ago
source link: https://www.geeksforgeeks.org/videos/c-program-to-check-if-a-given-number-is-ugly-number-or-not/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C++ Program to Check if a Given Number is Ugly Number or not

C++ Program to Check if a Given Number is Ugly Number or not
  • 990 Views
  • 29/07/2022

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/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK