Originally Posted by
annamayya
hiiiiiiii,
this program is about "FINDING THE 9 DIGIT NUMBER in which the number formed by leftmost two digits is divisible by 2,the number formed by left most three digits is divisible by 3 and SO on.......&at last the number is divisible by 9.......The condition is
no digit is used more than once"
[edit]
Post edited by Dave Evans. Thanks to istsme86 for pointing out my boo-boo.
Lots of statements that were reallllllly wrong have been deleted here
The smallest nine-digit number that has nine different non-zero digits is
123456789
The largest is
987654321
A brute force program does something like this:
Code:
int digits[9]
for (i = 123456789; i <= 987654321; i++) {
/* store the digits in digits[8], digits[7],... digits[0] */
/* see if the number qualifies: reject any number */
/* with digit[m] == 0, for m = 0 .. 9 */
/* for j = 0..8, k = 0..8, k != j */
/* reject any number with digit[j] == digit[k] */
/* then test the products; make a loop to reject any */
/* number for which ((digit[8]*10+digit[7] % 2) != 0 */
/* or (digit[8]*100+digit[7]*10 +digit[6])% 3) != 0 */
/* etc. */
/* print out successful candidates (or whatever) */
}
(Takes a long time to run, but it's easy to see that it works. If you think of a more optimal implementation somewhere along the way, then go for it!)
Of course, you can combine some of the steps, but keeping them separate like this may make it easier to debug. (And of course you do everything in loops instead of hard-coding a lot of comparisons.)
You can change the number of digits so that, for example, you can do some initial debugging with three-digit numbers or four-digit numbers. It's easier to see what went wrong if you print out intermediate steps (that is, if it doesn't work perfectly the first time).
[/edit]
Regards,
Dave