-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_NthNaturalNum.cpp
57 lines (47 loc) · 957 Bytes
/
GFG_NthNaturalNum.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
https://practice.geeksforgeeks.org/problems/nth-natural-number/1#
Nth Natural Number
*/
// { Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
// Convert to BASE 9
long long findNth(long long N)
{
if(N<9) return N;
long long ans;
long long rem;
int power=0;
long long mult=1;
while(N>0)
{
// rem = N%9;
// ans += rem*pow(10, power++);
// ans += (N%9)*pow(10, power++);
ans += (N%9)*mult;
mult *=10;
N = N/9; // quotient
}
return ans;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
long long n , ans;
cin>>n;
Solution obj;
ans = obj.findNth(n);
cout<<ans<<endl;
}
}
// } Driver Code Ends