C++ Examples

C++ to Check If Given Number is an Ugly number or not

The Program in C++ to Check If Given Number is an Ugly number or not is given below:

#include <iostream>

// Function to check if a number is an ugly number
bool isUgly(int num) {
  if (num <= 0) return false;
  if (num == 1) return true;
  
  while (num % 2 == 0) num /= 2;
  while (num % 3 == 0) num /= 3;
  while (num % 5 == 0) num /= 5;
  
  return num == 1;
}

int main() {
  int num;
  std::cout << "Hello Codeauri Family, enter a number here to know if it is ugly number or not!: ";
  std::cin >> num;
  
  // Check if the number is an ugly number
  if (isUgly(num)) {
    std::cout << num << " is an ugly number." << std::endl;
  } else {
    std::cout << num << " is not an ugly number." << std::endl;
  }
  
  return 0;
}

Output:

Hello Codeauri Family, enter a number here to know if it is ugly number or not!: 15
15 is an ugly number.

Pro-Tips💡

An ugly number is a number whose only prime factors are 2, 3, and 5.

The isUgly function takes a number as input and returns true if it is an ugly number, and false otherwise.

The function checks if the number is less than or equal to 0 or equal to 1, and returns the corresponding value.

If the number is not 1 or less than or equal to 0, the function divides the number by 2, 3, and 5 as many times as possible, and returns true if the resulting number is 1 and false otherwise.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X