diff --git a/Dynamic Programming/Ugly number.cpp b/Dynamic Programming/Ugly number.cpp new file mode 100644 index 0000000..01d7956 --- /dev/null +++ b/Dynamic Programming/Ugly number.cpp @@ -0,0 +1,57 @@ +# include +using namespace std; + +int min(int x, int y, int z) { //find smallest among three numbers + if(x < y) { + if(x < z) + return x; + else + return z; + }else { + if(y < z) + return y; + else + return z; + } +} + +int getUglyNum(int n) { + int uglyNum[n]; // To store ugly numbers + int i2 = 0, i3 = 0, i5 = 0; + + //find next multiple as 1*2, 1*3, 1*5 + + int next2mul = 2; + int next3mul = 3; + int next5mul = 5; + int next = 1; //initially the ugly number is 1 + + uglyNum[0] = 1; + + for (int i=1; i> n; + cout << n << "th Ugly number is: " << getUglyNum(n) << endl; +}