Friday, July 31, 2009

Help with c program...?

I have this question i need help solving.





Q)) Write a program to obtain all possible digit combinations for an entered number. (eg. entered no = 198 (combinations - 198,189,981,918,891,819))





This was my soln.





void main()


{


int a,b,c,d,e,f,i,j;


clrscr();





printf("Enter 3 digit no.");


scanf("%d",d);





a=d%10;


d=d/10;


b=d%10;


d=d/10;


c=d%10;





for(i=1;i%26lt;=3;i++)


{


for(j=1;j%26lt;=2;j++)


{


printf("%d%d%d",a,b,c);





e=b;


b=c;


c=e;


printf("%d%d%d",a,b,c);


}





f=a;


a=b;


b=f;





}


getch();


}





The solution is very bulky and it works only for a 3 digit no.





I would appreciate if anyone could suggest how to go about making it more simple and general. (btw no. of combinations for a number with 'x' digits is x factorial)

Help with c program...?
My only real idea would be to take the number they entered in and parse it down into an array. int nums[x] or whatever where x is the digit size of the number. Then with the array you can do basically the same thing as you did. That way it could handle any size number they input.





Let me know if that idea helps at all.


No comments:

Post a Comment