Skip to content

Commit e570656

Browse files
committed
maths
1 parent 69ca990 commit e570656

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Maths-DSA/Ugly-Number.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
An ugly number is a special type of number that can only be made by multiplying the numbers 2, 3, and 5 together. You start with the number 1, which is considered the first ugly number.
3+
*/
4+
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
bool isUgly(int n) {
11+
if(n == 1) return true;
12+
if(n < 1) return false;
13+
14+
if(n % 2 == 0) {
15+
return isUgly(n / 2);
16+
}
17+
if(n % 3 == 0) {
18+
return isUgly(n / 3);
19+
}
20+
if(n % 5 == 0) {
21+
return isUgly(n / 5);
22+
}
23+
return false;
24+
}
25+
};
26+
27+
int main() {
28+
Solution solution;
29+
int n;
30+
31+
cout << "Enter a number to check if it is an ugly number: ";
32+
cin >> n;
33+
34+
if (solution.isUgly(n)) {
35+
cout << n << " is an ugly number." << endl;
36+
} else {
37+
cout << n << " is not an ugly number." << endl;
38+
}
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)