# Program to find N-th term of series 3, 5, 33, 35, 53….

Given a series of numbers composed only of digits 3 and 5. The first few numbers in the series are:

3, 5, 33, 35, 53, 55, …..

Given a number N. The task is to find the n-th number in the given series.

Examples:

```Input : N = 2
Output : 5

Input : N = 5
Output : 53
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is based on the fact that the value of the last digit alternates in the series. For example, if the last digit of ith number is 3, then the last digit of (i-1)th and (i+1)th numbers must be 5.

Create an array of size (n+1) and push 3 and 5(These two are always first two elements of series) to it. For more elements check,

```1) If i is odd,
arr[i] = arr[i/2]*10 + 3;
2) If it is even,
arr[i] = arr[(i/2)-1]*10 + 5;
At last return arr[n].
```

Below is the implementation of the above idea:

## C++

 `// C++ program to find n-th number in a series ` `// made of digits 3 and 5 ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find n-th number in series ` `// made of 3 and 5 ` `int` `printNthElement(``int` `n) ` `{ ` `    ``// create an array of size (n+1) ` `    ``int` `arr[n + 1]; ` `    ``arr = 3; ` `    ``arr = 5; ` ` `  `    ``for` `(``int` `i = 3; i <= n; i++) { ` `        ``// If i is odd ` `        ``if` `(i % 2 != 0) ` `            ``arr[i] = arr[i / 2] * 10 + 3; ` `        ``else` `            ``arr[i] = arr[(i / 2) - 1] * 10 + 5; ` `    ``} ` `    ``return` `arr[n]; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 6; ` ` `  `    ``cout << printNthElement(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find n-th number in a series ` `// made of digits 3 and 5 ` ` `  `class` `FindNth { ` `    ``// Function to find n-th number in series ` `    ``// made of 3 and 5 ` `    ``static` `int` `printNthElement(``int` `n) ` `    ``{ ` `        ``// create an array of size (n+1) ` `        ``int` `arr[] = ``new` `int``[n + ``1``]; ` `        ``arr[``1``] = ``3``; ` `        ``arr[``2``] = ``5``; ` ` `  `        ``for` `(``int` `i = ``3``; i <= n; i++) { ` `            ``// If i is odd ` `            ``if` `(i % ``2` `!= ``0``) ` `                ``arr[i] = arr[i / ``2``] * ``10` `+ ``3``; ` `            ``else` `                ``arr[i] = arr[(i / ``2``) - ``1``] * ``10` `+ ``5``; ` `        ``} ` `        ``return` `arr[n]; ` `    ``} ` ` `  `    ``// main function ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``6``; ` ` `  `        ``System.out.println(printNthElement(n)); ` `    ``} ` `} `

## Python3

 `# Python3 program to find n-th number   ` `# in a series made of digits 3 and 5  ` `   `  `# Return n-th number in series made   ` `# of 3 and 5  ` `def` `printNthElement(n) :  ` `       `  `    ``# create an array of size (n + 1)  ` `    ``arr ``=``[``0``] ``*` `(n ``+` `1``);  ` `    ``arr[``1``] ``=` `3` `    ``arr[``2``] ``=` `5` `   `  `    ``for` `i ``in` `range``(``3``, n ``+` `1``) :  ` `        ``# If i is odd  ` `        ``if` `(i ``%` `2` `!``=` `0``) :  ` `            ``arr[i] ``=` `arr[i ``/``/` `2``] ``*` `10` `+` `3` `        ``else` `:  ` `            ``arr[i] ``=` `arr[(i ``/``/` `2``) ``-` `1``] ``*` `10` `+` `5` `       `  `    ``return` `arr[n]  ` `       `  `# Driver code  ` `n ``=` `6` `print``(printNthElement(n))  `

## C#

 `// C# program to find n-th number  ` `// in a series made of digits 3 and 5  ` `using` `System; ` ` `  `class` `GFG ` `{  ` `// Function to find n-th number  ` `// in series made of 3 and 5  ` `static` `int` `printNthElement(``int` `n)  ` `{  ` `    ``// create an array of size (n+1)  ` `    ``int` `[] arr = ``new` `int``[n + 1];  ` `    ``arr = 3;  ` `    ``arr = 5;  ` ` `  `    ``for` `(``int` `i = 3; i <= n; i++) ` `    ``{  ` `        ``// If i is odd  ` `        ``if` `(i % 2 != 0)  ` `            ``arr[i] = arr[i / 2] * 10 + 3;  ` `        ``else` `            ``arr[i] = arr[(i / 2) - 1] * 10 + 5;  ` `    ``}  ` `    ``return` `arr[n];  ` `}  ` ` `  `// Driver Code ` `static` `void` `Main()  ` `{  ` `    ``int` `n = 6;  ` ` `  `    ``Console.WriteLine(printNthElement(n));  ` `}  ` `}  ` ` `  `// This code is contributed by ANKITRAI1 `

## PHP

 ` `

Output:

```55
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01, chitranayal

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.