We can approach the problem by analyzing the requirements.
So, we need odd digits. There are only 5 odd digits: 1,3,5,7,9. Any number that we create can consist only of those 5 digits.
The second observation is that there are 5^(NumberOfDigits) different numbers. That is easy to understand. Example: For the first digit, we have 5 possibilities. And then, for each of this 5 digits we have again 5 possible second digits. So, now already 5*5=25
numbers. And for each of the 5 second digits we have again 5 third digits, so, 125 numbers. And for a 4 digit number, we have 5*5*5*5=5^4=625
different numbers.
Good, how to find them? It is obvious, that the delta between two odd digits is 2. Or with other words, if we have an odd digit, we can simply add 2 to get the next odd digit. After the 9, we will have an overflow and need to continue again with 1.
There are many potential designs for the solution.
I will show here an example, were I do not use a complete number, but an array of digits. Then I will add 2 to one digit and if there is an overflow, I will also add 2 to the next digit and so on and so on. Somehow simple.
We do this, until the most significant digit will flip to one, and then all digits are 1 again.
#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>
#include <iomanip>
// Number of digits in a number
constexpr size_t NumberOfDigits = 4u;
// Representation of the number as an array of digits
using Digits = std::array<int, NumberOfDigits>;
// Get the next number/digits
constexpr void nextDigits(Digits& digits) noexcept {
// Starting at the least significant digit
for (int i{ NumberOfDigits-1 }; i >= 0; --i) {
// Get the next odd number
digits[i] += 2;
// Check for overflow, then reset to 1, else stop
if (digits[i] > 9) digits[i] = 1;
else break;
}
}
int main() {
// Define our array with digits and fill it with all one
Digits d{};
std::fill(d.begin(), d.end(), 1);
// Some counter, Just for display purposes
unsigned long long i = 1;
do {
// Some debug output
std::cout << std::right << std::setw(30) << i++ << " --> ";
std::copy(d.begin(), d.end(), std::ostream_iterator<int>(std::cout));
std::cout << '\n';
// And, get the next number/digits
nextDigits(d);
// Do this, until we have found all numbers
} while (not std::all_of(d.begin(), d.end(), [](const int i) {return i == 1; }));
}
Again, this is one of many potential solutions