UGLY number

PROBLEM :

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find Nth Ugly Number.

Input:

The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.

Output:

Print the Nth Ugly Number.

Constraints:

1 = T = 100
1 = N = 500

Example:

Input:
2
10
4

Output:
12
4


------------------------------------------------------------------------------
//SIMPLE C++ IMPLEMENTATION
------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int ugly_no_check(int) ;
int main()
 {
int test,no,ele,count,r ;
cout<<"\n Enter number of test cases : " ;
cin>>test ;
while(test--)
{
cout<<"\n enter the N'th position "  ;
   cin>>no ;
   count=1 ;
   ele=1 ;
   while(count!=no)
   {
    ele++ ;
      r=ugly_no_check(ele) ;
      if(r==1)
           count++  ;
   }
   cout<<"\n Ugly number at "<<no<<"'th position is " ;
   cout<<ele<<endl ;
}
return 0;
}

int ugly_no_check(int no)
{
    while(no%2==0)
        no=no/2 ;
     
    while(no%3==0)
        no=no/3 ;
     
    while(no%5==0)
        no=no/5 ;
     
    return no  ;
}

-----------------------------------------------------------------------------

Comments