# Find position of given term in a series formed with only digits 4 and 7 allowed

There is a series of numbers that have only digits, 4 and 7, and numbers are arranged in increasing order. The first few numbers of the series are **4, 7, 44, 47, 74, 77, 444, …etc.** Given a number **N**, the task is to find the position of that number in the given series.

**Examples:**

Input:N = 4

Output:1

Explanation:

The first number in the series is 4

Input:N = 777

Output:14

Explanation:

The 14th number in the series is 777

**Approach:** This problem can be solved using the below observations:

- Below is the pattern observe in the given series. The numbers can be seen as:
"" / \ 4 7 / \ / \ 44 47 74 77 / \ / \ / \ / \

- As we can observe the pattern is increasing in power of
**2**. Therefore the idea is to iterate over the digits of the number starting from the least significant digit and update the position of the number as:- If current digit = 7, then add to the position.
- If current digit = 4, then add to the position.

- Print the final position after the above operations.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the position ` `// of the number N ` `void` `findPosition(` `int` `n) ` `{ ` ` ` `int` `i = 0; ` ` ` ` ` `// To store the position of N ` ` ` `int` `pos = 0; ` ` ` ` ` `// Iterate through all digit of N ` ` ` `while` `(n > 0) { ` ` ` ` ` `// If current digit is 7 ` ` ` `if` `(n % 10 == 7) { ` ` ` `pos = pos + ` `pow` `(2, i + 1); ` ` ` `} ` ` ` ` ` `// If current digit is 4 ` ` ` `else` `{ ` ` ` `pos = pos + ` `pow` `(2, i); ` ` ` `} ` ` ` ` ` `i++; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// Print the final position ` ` ` `cout << pos; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given number of the series ` ` ` `int` `N = 777; ` ` ` ` ` `// Function Call ` ` ` `findPosition(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to find the position ` `// of the number N ` `static` `void` `findPosition(` `int` `n) ` `{ ` ` ` `int` `i = ` `0` `; ` ` ` ` ` `// To store the position of N ` ` ` `int` `pos = ` `0` `; ` ` ` ` ` `// Iterate through all digit of N ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` ` ` `// If current digit is 7 ` ` ` `if` `(n % ` `10` `== ` `7` `) ` ` ` `{ ` ` ` `pos = pos + (` `int` `)Math.pow(` `2` `, i + ` `1` `); ` ` ` `} ` ` ` ` ` `// If current digit is 4 ` ` ` `else` ` ` `{ ` ` ` `pos = pos + (` `int` `)Math.pow(` `2` `, i); ` ` ` `} ` ` ` ` ` `i++; ` ` ` `n = n / ` `10` `; ` ` ` `} ` ` ` ` ` `// Print the final position ` ` ` `System.out.print(pos); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Given number of the series ` ` ` `int` `N = ` `777` `; ` ` ` ` ` `// Function Call ` ` ` `findPosition(N); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find the position ` `# of the number N ` `def` `findPosition(n): ` ` ` ` ` `i ` `=` `0` ` ` ` ` `# To store the position of N ` ` ` `pos ` `=` `0` ` ` ` ` `# Iterate through all digit of N ` ` ` `while` `(n > ` `0` `): ` ` ` ` ` `# If current digit is 7 ` ` ` `if` `(n ` `%` `10` `=` `=` `7` `): ` ` ` `pos ` `=` `pos ` `+` `pow` `(` `2` `, i ` `+` `1` `) ` ` ` ` ` `# If current digit is 4 ` ` ` `else` `: ` ` ` `pos ` `=` `pos ` `+` `pow` `(` `2` `, i) ` ` ` ` ` `i ` `+` `=` `1` ` ` `n ` `=` `n ` `/` `/` `10` ` ` ` ` `# Print the final position ` ` ` `print` `(pos) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given number of the series ` ` ` `N ` `=` `777` ` ` ` ` `# Function Call ` ` ` `findPosition(N) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the position ` `// of the number N ` `static` `void` `findPosition(` `int` `n) ` `{ ` ` ` `int` `i = 0; ` ` ` ` ` `// To store the position of N ` ` ` `int` `pos = 0; ` ` ` ` ` `// Iterate through all digit of N ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` ` ` `// If current digit is 7 ` ` ` `if` `(n % 10 == 7) ` ` ` `{ ` ` ` `pos = pos + (` `int` `)Math.Pow(2, i + 1); ` ` ` `} ` ` ` ` ` `// If current digit is 4 ` ` ` `else` ` ` `{ ` ` ` `pos = pos + (` `int` `)Math.Pow(2, i); ` ` ` `} ` ` ` ` ` `i++; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// Print the final position ` ` ` `Console.Write(pos); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `// Given number of the series ` ` ` `int` `N = 777; ` ` ` ` ` `// Function Call ` ` ` `findPosition(N); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

14

**Time Complexity:** *O(log _{10}N)*

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find n-th element in a series with only 2 digits (4 and 7) allowed
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Find the Nth term of the series 2 + 6 + 13 + 23 + . . .
- Find Nth term of the series 2, 3, 10, 15, 26....
- Find Nth term of the series 1, 8, 54, 384...
- Find n-th term in the series 7, 15, 32, ...
- Find Nth term of the series 1, 1, 2, 6, 24...
- Find n-th term in the series 9, 33, 73,129 ...
- Find the Nth term in series 12, 35, 81, 173, 357, ...
- Find Nth term of the series 0, 6, 0, 12, 0, 90...
- Find Nth term of the series 4, 2, 2, 3, 6, ...
- Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 ...
- Find n-th term of series 1, 3, 6, 10, 15, 21...
- Find Nth term of the series 1, 6, 18, 40, 75, ....
- Find Nth term of the series 1, 5, 32, 288 ...
- Find n-th term in series 1 2 2 3 3 3 4 4 4 4....
- Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ...

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.