- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find position of the given number among the numbers made of 4 and 7 in C++
In this problem we are given a number N. Our task is to Find position of the given number among the numbers made of 4 and 7. The series consisting of 4 and 7 only is 4, 7, 44, 47, 74, 77, 444….
Let’s take an example to understand the problem,
Input
N = 5
Output
74
Explanation
Series upto 5 terms is 4, 7, 44, 47, 74…
Solution Approach
A simple solution to the problem is based on finding the pattern in the series.
Here, every even position contains 7 in the end.
And every odd position contains 4 in the end.
So, we can find the series by going digit by digit and finding the position based on the current digit.
If the current digit is 4, the position will be updated as position = (position*2) + 1.
If the current digit is 7, the position will be updated as position = (position*2) + 2.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int findNumPosition(string num){ int i = 0, position = 0; while (num[i] != '\0') { position *= 2; if(num[i] == '4') position += 1; else position += 2; i++; } return position; } int main() { string num = "74774"; cout<<"The position of the number in the series is "<<findNumPosition(num); return 0; }
Output
The position of the number in the series is 53
- Related Articles
- Find number of unique triangles among given N triangles in C++
- C program to Find the Largest Number Among Three Numbers
- Number of digits in the nth number made of given four digits in C++
- C++ Program to Find Largest Number Among Three Numbers
- Among the numbers given which number is the smallest ?$frac{3}{5}$, $frac{2}{3}$, $frac{22}{7}$,and1.
- Find the difference between the smallest number of 7 digits and the largest number of 4 digits.
- Python - Find the number of prime numbers within a given range of numbers
- Update the bit in the given position or Index of a number using C++
- The sum of three numbers in A.P. is $30$ and the ratio of the first number to the third the number is $3:7$. Find the numbers.
- Find the number of numbers which are divisible by $7$ between $100$ and $1000$.
- Average of 12 numbers is 48 average of the first 7 numbers is 60 and the average of last 4 numbers is 22 then the eighth number is____.
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- Find the number of different numbers in the array after applying the given operation q times in C++
- Find the Largest number with given number of digits and sum of digits in C++
- The sum of a number and its square is $frac{63}{4}$, find the numbers.

Advertisements