top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

print all the combinations of the digits of a given number

+5 votes
541 views

Algorithm to print all the number possible with the digits of a given number

eg :if input is "234",combinations possible are

234,243,324,342,432,423

posted Oct 12, 2013 by Sony Mohanty

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Total number of combinations are Factorial N, this is what you are looking or wants to print the numbers also - assuming all distinct digits.

2 Answers

+5 votes
void permute(int a[], int i, int N)
{
        int j;
        if(i == N)
                printf("\n%d%d%d%d",a[0],a[1],a[2],a[3]); 
        else
                for ( j = i; j < N; j++)
                {
                        a[i] = a[i] + a[j] - (a[j] = a[i]);
                        permute(a, i + 1, N);
                        a[i] = a[i] + a[j] - (a[j] = a[i]);
                }
}

int main()
{
        int a[] = {1,2,3,4};
        permute(a,0,4);
        return 0;
}
answer Oct 13, 2013 by Vikas Upadhyay
Well done Vikas.
0 votes

Try this (in C++ though u can write it again in C)

#include <iostream>
#include <algorithm>
using namespace std;

void outputArray(int* array, int size)
{
  for (int i = 0; i < size; ++i) { cout << array[i] << " "; }
}

int main ()
{
  int myints[] = { 1, 2, 3, 4, 5 };
  const int size = sizeof(myints);

  cout << "The 5! possible permutations with 5 elements:\n";

  sort (myints, myints + size);

  bool hasMorePermutations = true;
  do
  {
    outputArray(myints, size);
    hasMorePermutations = next_permutation(myints, myints + size);
  }
  while (hasMorePermutations);

  return 0;
}
answer Oct 12, 2013 by Deepankar Dubey
where is this function "next_permutation" implemented.
Similar Questions
+3 votes

A number is called as a Jumping Number if all adjacent digits in it differ by 1. The difference between ‘9’ and ‘0’ is not considered as 1.
All single digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not.

Given a positive number x, print all Jumping Numbers smaller than or equal to x. The numbers can be printed in any order.

Example:

Input: x = 20
Output: 0 1 2 3 4 5 6 7 8 9 10 12

Input: x = 105
Output: 0 1 2 3 4 5 6 7 8 9 10 12
21 23 32 34 43 45 54 56 65
67 76 78 87 89 98 101

Note: Order of output doesn't matter,
i,e., numbers can be printed in any order

+7 votes

Print cousins of a given node (Not sibling) ??

+6 votes

How can we count Number of Non Leaf Nodes of a given Tree ?

Example
            40
            /\
           /  \
         20    60
         / \    \
       10  30   80
                 \
                 90

Answer => 4

...