
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find n-th element in a series with only 2 digits (and 7) allowed in C++
In this problem, we are given an integer N, denoting a series of numbers consisting of 4 and 7 only.
The series is 4, 7, 44, 47, 74, 77, …
The task is to find the n-th element in a series with only 2 digits (and 7) allowed.
Let’s take an example to understand the problem,
Input
N = 4,
Output
47
Explanation
The series is: 4, 7, 44, 47, ….
Solution Approach
A simple solution to the problem is creating the series till Nth number. It’s simple, if the current number’s last digit is 7. Then the last digit of previous and next numbers is 4.
So, we will start from 1st and 2nd numbers and then progress to the next element.
For this, we will create an array series[n+1].
For index series[1] put 4 For index series[2] put 7
Then for successive values till N, find the values for the given index i,
If i is odd, series[i] = series[i/2]*10 + 4 If i is even, series[i] = series[i/2]*10 + 7
After n iterations, return value at series[n].
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int findNthSeriesElement(int N) { int series[N+1]; series[1] = 4; series[2] = 7; for (int i=3; i<=N; i++) { if (i%2 != 0) series[i] = series[i/2]*10 + 4; else series[i] = series[(i/2)-1]*10 + 7; } return series[N]; } int main() { int N = 9; cout<<"The "<<N<<"th element of the array is "<<findNthSeriesElement(N); return 0; }
Output
The 9th element of the array is 474
- Related Articles
- Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++
- Find n-th element from Stern’s Diatomic Series in C++
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- C++ program to find n-th term in the series 7, 15, 32, …
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Program to find N-th term of series 0, 2,1, 3, 1, 5, 2, 7, 3...in C++
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
- C++ Programe to find n-th term in series 1 2 2 3 3 3 4
- Program to find N-th term of series 7, 21, 49, 91, 147, 217, …… in C++
- Program to find multiple of n with only two digits in Python
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Minimum number with digits as and 7 only and given sum in C++
- n-th term in series 2, 12, 36, 80, 150….in C++

Advertisements