-5

Can anyone help me with a C++ code that prints out all the possible permutations of the given number.

For example, if the number N = 123, then {123, 132, 213, 231, 312, 321} are the possible permutations.

I have researched and could find the code only for string and not an integer.

Thanks.

Anant Mathur
  • 13
  • 1
  • 10

1 Answers1

4

You might use:

void display_permutation(std::size_t n)
{
    std::string s = std::to_string(n);
    std::sort(s.begin(), s.end());
    do {
        std::cout << s << std::endl;
    } while (std::next_permutation(s.begin(), s.end()));
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thanks. But I could not get your point. Can you please elaborate your code? – Anant Mathur Jan 11 '19 at 03:07
  • Which parts is unclear for you ? Have you look to documentation of [std::to_string](https://en.cppreference.com/w/cpp/string/basic_string/to_string), [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort) and especially of [std::next_permutation](https://en.cppreference.com/w/cpp/algorithm/next_permutation) ? – Jarod42 Jan 11 '19 at 11:08
  • Got it now. Thanks! – Anant Mathur Jan 11 '19 at 13:57