-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode_1.cpp
43 lines (39 loc) · 806 Bytes
/
code_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// main.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 31/07/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
// CPP program to find nth ugly number
#include<iostream>
using namespace std;
int maxDivide(int a, int b) {
while (a%b == 0) {
a = a/b;
}
return a;
}
int isUgly(int num) {
num = maxDivide(num, 2);
num = maxDivide(num, 3);
num = maxDivide(num, 5);
return (num == 1)? 1 : 0;
}
int getNthUglyNo(int n) {
int i = 1;
int count = 1;
while (n > count) {
i++;
if (isUgly(i))
count++;
}
return i;
}
int main() {
cout << "\nEnter Number\t:\t";
unsigned int number;
cin >> number;
cout <<"\nThe result is\t:\t" << getNthUglyNo(number);
cout << endl;
return 0;
}