Programmer

Thursday 7 June 2018

Program to Generate All Possible Combinations of a Given Number.

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
long n, x, arr[10],i=0,count=0, size=0, temp, start, stop;
long tempArr[10],flag[10]={0};
clrscr();

cout<<"Enter N : ";
cin>>n;
x=n;
//Loop for getting the length of digits.
while(x)
{
temp=x%10;
size++;
arr[i++]=temp;
x/=10;
}

start=pow(10,size-1);
stop=pow(10,size);

//Loop for printing the range of length possibilities.
for(i=start;i<stop;i++)
{
long temp=i;
for(int j=0;j<size;j++)
{
tempArr[j]=temp%10;
temp=temp/10;
}
for(j=0;j<size;j++)
{
flag[j]=0;
for(int k=0;k<size;k++)
{
if(arr[j]==tempArr[k])
flag[j]+=1;
}
}
int f=0;
for(j=0;j<size;j++)
{
if(flag[j]!=1)
{
f++;
break;
}
}
if(!f)
{
cout<<i<<"\t";
count++;
}
}
cout<<"\nThere are "<<count<<" possibilities\n";

getch();
}


No comments:

Post a Comment

Program To Print Particular Line From File.

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { ifstream fin; int count=0,num,l=1; char ...