# Find position of the given number among the numbers made of 4 and 7

Consider a series of numbers composed of only digits 4 and 7. The first few numbers in the series are 4, 7, 44, 47, 74, 77, 444, .. etc. Given a number constructed by 4, 7 digits only, we need to find the position of this number in this series.

Examples:

Input : 7 Output : pos = 2 Input : 444 Output : pos = 7

It is reverse of the following article :

Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)

"" / \ 1(4) 2(7) / \ / \ 3(44) 4(47) 5(74) 6(77) / \ / \ / \ / \

The idea is based on the fact that all even positioned numbers have 7 as the last digit and all odd positioned numbers have 4 as the last digit.

If the number is 4 then it is the left node of the tree, then it corresponds to (pos*2)+1. Else right child node(7) corresponds to (pos*2)+2.

## C++

`// C++ program to find position of a number ` `// in a series of numbers with 4 and 7 as the ` `// only digits. ` `#include <iostream> ` `#include <algorithm> ` `using` `namespace` `std; ` ` ` `int` `findpos(string n) ` `{ ` ` ` `int` `i = 0, pos = 0; ` ` ` `while` `(n[i] != ` `'\0'` `) { ` ` ` ` ` `// check all digit position ` ` ` `switch` `(n[i]) ` ` ` `{ ` ` ` ` ` `// if number is left then pos*2+1 ` ` ` `case` `'4'` `: ` ` ` `pos = pos * 2 + 1; ` ` ` `break` `; ` ` ` ` ` `// if number is right then pos*2+2 ` ` ` `case` `'7'` `: ` ` ` `pos = pos * 2 + 2; ` ` ` `break` `; ` ` ` `} ` ` ` `i++; ` ` ` `} ` ` ` `return` `pos; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// given a number which is constructed ` ` ` `// by 4 and 7 digit only ` ` ` `string n = ` `"774"` `; ` ` ` `cout << findpos(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to find position of a number ` `// in a series of numbers with 4 and 7 as the ` `// only digits. ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `findpos(String n) ` ` ` `{ ` ` ` ` ` `int` `k = ` `0` `, pos = ` `0` `, i = ` `0` `; ` ` ` `while` `(k != n.length()) { ` ` ` ` ` `// check all digit position ` ` ` `switch` `(n.charAt(i)) { ` ` ` ` ` `// if number is left then pos*2+1 ` ` ` `case` `'4'` `: ` ` ` `pos = pos * ` `2` `+ ` `1` `; ` ` ` `break` `; ` ` ` ` ` `// if number is right then pos*2+2 ` ` ` `case` `'7'` `: ` ` ` `pos = pos * ` `2` `+ ` `2` `; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `i++; ` ` ` `k++; ` ` ` `} ` ` ` ` ` `return` `pos; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// given a number which is constructed ` ` ` `// by 4 and 7 digit only ` ` ` `String n = ` `"774"` `; ` ` ` ` ` `System.out.println(findpos(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## Python3

`# python program to find position ` `# of a number in a series of ` `# numbers with 4 and 7 as the ` `# only digits. ` `def` `findpos(n): ` ` ` `i ` `=` `0` ` ` `j ` `=` `len` `(n) ` ` ` `pos ` `=` `0` ` ` `while` `(i<j): ` ` ` ` ` `# check all digit position ` ` ` `# if number is left then ` ` ` `# pos*2+1 ` ` ` `if` `(n[i] ` `=` `=` `'4'` `): ` ` ` `pos ` `=` `pos ` `*` `2` `+` `1` ` ` ` ` `# if number is right then ` ` ` `# pos*2+2 ` ` ` `if` `(n[i] ` `=` `=` `'7'` `): ` ` ` `pos ` `=` `pos ` `*` `2` `+` `2` ` ` ` ` `i` `=` `i` `+` `1` ` ` ` ` `return` `pos ` ` ` ` ` `# Driver code ` `# given a number which is constructed ` `# by 4 and 7 digit only ` `n ` `=` `"774"` `print` `(findpos(n)) ` ` ` `# This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find position of ` `// a number in a series of numbers ` `// with 4 and 7 as the only digits. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `findpos(String n) ` ` ` `{ ` ` ` ` ` `int` `k = 0, pos = 0, i = 0; ` ` ` `while` `(k != n.Length) { ` ` ` ` ` `// check all digit position ` ` ` `switch` `(n[i]) { ` ` ` ` ` `// if number is left then pos*2+1 ` ` ` `case` `'4'` `: ` ` ` `pos = pos * 2 + 1; ` ` ` `break` `; ` ` ` ` ` `// if number is right then pos*2+2 ` ` ` `case` `'7'` `: ` ` ` `pos = pos * 2 + 2; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `i++; ` ` ` `k++; ` ` ` `} ` ` ` ` ` `return` `pos; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `// given a number which is constructed ` ` ` `// by 4 and 7 digit only ` ` ` `String n = ` `"774"` `; ` ` ` ` ` `Console.Write(findpos(n)); ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find position of a number ` `// in a series of numbers with 4 and 7 as the ` `// only digits. ` ` ` `function` `findpos(` `$n` `) ` `{ ` ` ` `$i` `= 0; ` ` ` `$pos` `= 0; ` ` ` `while` `(` `$i` `< ` `strlen` `(` `$n` `)) { ` ` ` ` ` `// check all digit position ` ` ` `switch` `(` `$n` `[` `$i` `]) ` ` ` `{ ` ` ` ` ` `// if number is left then pos*2+1 ` ` ` `case` `'4'` `: ` ` ` `$pos` `= ` `$pos` `* 2 + 1; ` ` ` `break` `; ` ` ` ` ` `// if number is right then pos*2+2 ` ` ` `case` `'7'` `: ` ` ` `$pos` `= ` `$pos` `* 2 + 2; ` ` ` `break` `; ` ` ` `} ` ` ` `$i` `++; ` ` ` `} ` ` ` `return` `$pos` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `// given a number which ` ` ` `// is constructed by 4 ` ` ` `// and 7 digit only ` ` ` `$n` `= ` `"774"` `; ` ` ` `echo` `findpos(` `$n` `); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

Output:

13

This article is contributed by **Devanshu Agarwal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Position of n among the numbers made of 2, 3, 5 & 7
- Find two numbers made up of a given digit such that their difference is divisible by N
- Find the n-th number made of even digits only
- Find the position of the given Prime Number
- Find the number of occurrences of a character upto preceding position
- N-th character in the string made by concatenating natural numbers
- Check if elements of array can be made equal by multiplying given prime numbers
- Sum of all the prime numbers with the maximum position of set bit ≤ D
- Nth number made up of odd digits only
- Find the character made by adding all the characters of the given string
- Find the longest string that can be made up of other strings from the array
- Largest number divisible by 90 that can be made using 0 and 5
- Find if given number is sum of first n natural numbers
- Total number of different staircase that can made from N boxes
- Check whether the number can be made palindromic after adding K
- Largest number made up of X and Y with count of X divisible by Y and of Y by X
- Check if X and Y can be made zero by using given operation any number of times
- Count number of strings (made of R, G and B) using given combination
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Find the number of divisors of all numbers in the range [1, n]