How to print all the combinations of a given integer

Suppose i have given like integer 123.....then print like all the combinations 111,112,113,121......up to 333..

Questions by bajjuri.thirupathi

Showing Answers 1 - 9 of 9 Answers

This program works fine only for a number containing three digits.

But i prefer to intake these three digits at the time of scanning as a string rather than a single number for the flexibility of the program.


#include<string.h>
#include<stdio.h>


main()
{
char string_of_integers[4];
char first=0;
char second=0;
char third=0;


printf("nGive any three digit number");
scanf("n%s",string_of_integers);


printf("nThe required combinations are:n")


for(first=0;first<=2;first++)
 for(second=0;second<=2;second++)
    for(third=0;third<=2;third++)
       printf("%c%c%ct",string_of_integers[first],string_of_integers[second],string_of_integers[third]);


printf("nThis is the end of the program");
getch();

}

Expand the logic to make it work for more number of digits and also use some additional verificational logic if you are to work with varying number of digits, i mean you need to add extra logic if at all flexibility needs to be a attribute of the program.

  Was this answer useful?  Yes

ravi_529

  • Dec 24th, 2007
 

int main(int argc,char* argv[])
{
int i,j,k;
int n,r;
int dig[3];

 printf("enter any three dig numbern");
 scanf("%d",&n);

  dig[2]=n%10;
  n=n/10;
  dig[1]=n%10;
  n=n/10;
  dig[0]=n%10;

  for(i=0;i<3;i++)
  for(j=0;j<3;j++)
  for(k=0;k<3;k++)
  printf("%d%d%dn",dig[i],dig[j],dig[k]);

  return 0;
  }

  Was this answer useful?  Yes

bathesabari

  • Feb 17th, 2009
 

/*to print possible patern using given no*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[100],b[100],n,x=1,y,k,j,i,m,c;
 clrscr();
 printf("enter no of nost");
 scanf("%d",&n);
 m=n-1;
 printf("nenter nost");
 for(i=0,c=m;i<n;i++,c--)
 {
 printf("n");
 scanf("%d",&a[i]);
 b[c]=a[i];
 }
 for(i=1;i<=n;i++)
 { x=x*i; }
 printf("%dnnn",x);

 y=x/2;
 for(i=0;i<x;i++)
 {
  if(i<y)
  {
   for(j=0;j<n;j++)
   {
    if(j==0)
     a[n]=a[0];
    k=j+1;
    a[j]=a[k];
    printf("  %d ",a[j]);
   }


  }
  else
  {

   for(j=0;j<n;j++)
   {
    if(j==0)
    b[n]=b[0];
    k=j+1;
    b[j]=b[k];
    printf("  %d ",b[j]);
   }
  }
  printf("nn");
      }
 getch();
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions