For this project I need to generate all three digit numbers where the 1st digit is always larger than the 2nd, and the 2nd digit is always larger than the third.

I'm not asking you guys to do it for me but to point me in the right direction.

I'm guessing the easier way of doing this is by using Array, but how would I right out the formula to not just generate all 100-999 numbers but where the 1st digit is always larger than the 2nd and 2nd digit is always larger than the third.

Thanks

4
Contributors
14
Replies
21
Views
6 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
• 1

I understood it to be numbers like 321 or 987, but not 123 or 999. I think there are 120 3-digit numbers that meet the definition and 880 that don't. I would use 3 nested for loops - one for each of the three digits, although it's possible with while …

Here's what I got so far. I only made a array table of a 300x3 table. Since there's 900 3 digit numbers. I wasn't able to print it out in the order of 1st # > 2nd # > 3rd #. But i thought i'll handle things one at a time.

``````import java.util.*;

public class ThreeDigit
{
// Constants.
public static final int ARRAY_SIZE = 900;
public static final int MAX_NUMBER = 999;

public static void main( String[] args )
{
// Create an empty set to store the unique random numbers.

// Create a random number generator.
Random random = new Random();

// While the set is smaller than the required size (900)...
while( numberSet.size() < ARRAY_SIZE )
{
numberSet.add( random.nextInt( MAX_NUMBER + 1) );
}

// Convert the set into an array.
Integer[] numbers = numberSet.toArray( new Integer[ 0 ] );

// Print out the numbers in a 300 x 3 grid.
for( int row = 0; row < 300; ++row )
{
for( int col = 0; col < 3; ++col )
{
int index = ( row * 3 ) + col;
System.out.printf( "%03d ", numbers[ index ] );
}
System.out.println();
}
}
}``````

Does anyone know how do i make the program pick a number between 100-999 instead of 0-999?

Does anyone know how do i make the program pick a number between 100-999 instead of 0-999?

100 + random.nextInt(900)

thanks for the reply. my teacher replied to my email and i ended up doing the program wrong :/ no random numbers.

"Examine all numbers from 000 to 999. For each number (000,001,002,003....321,322,......985,986,...999) check if the hundreds digit is greater than the tens digit and that the tens digit is greater than the ones digit. For example, print out 210, ..,321,...432...987. You'll want to do this using nested loops."

so i guess i have to start over D: so what i'm doing is having 3 variables let's say n1, n2, n3? and then comparing which is greater.

so i wouldn't be using arrays?

Edited by cecsFTL: n/a

Yes. The clue is in "You'll want to do this using nested loops." Each loop requires its own loop variable, so your n1, n2, n3 will correspond to 3 nested loops. This won't use any arrays.

What's the code to put limits? I did a similar lab assignment but forgot.

So basically I do the nested loops and then put it in a string? What's the best way to print it since I won't need arrays? Or when it prints it will print 1 3 digit and next line. That's a whole lot of numbers!

Also, how do I make sure that in these 999 numbers that no 3 digit numbers are repeated? Or it doesn't matter if it repeats.

Each digit must be the range 0-9, so those are the limits for each loop (although you could optimise this a bit*). When you get a number (set of 3 digits) in the innermost loop that meets the requirements you print them. If you do the loops right the innermost one will execute 10x10x10 times, generating each number from 000 to 999 without any repetitions.

* since for all the numbers you are interested in n1 > n2 > n3, you can skip n1 = 0 or n1 = 1, and n2 = 0, because they cannot meet your requirement

Oh I see! Haha.. I thought it was to be 400 > 300 > 100, but it's ten digits. I would be using the while loop?

Sorry for all the questions :p I appreciate your responses! It's just that nothing my teacher say is on the test so I don't bother to listen and just stick to the book.

I understood it to be numbers like 321 or 987, but not 123 or 999. I think there are 120 3-digit numbers that meet the definition and 880 that don't.
I would use 3 nested for loops - one for each of the three digits, although it's possible with while loop(s) as well, if you prefer.

I think this is correct? It's a very short code so can you guys read and remove or add what's necessary?

``````public class ThreeDigit
{
public static void main (String[] args)
{
for(int n1 = 0; n1 < 9; n1++)
{
for(int n2 = 0; n2 < 9; n2++)
{
for(int n3 = 0; n3 < 9; n3++)
{
if(n1 > n2 && n2 > n3)
{
System.out.print(n1);
System.out.print(n2);
System.out.println(n3);
}
}
}
}
}
}``````

It prints out around 130 like you said. Is there any thing wrong or is there a easier way to do it?

That's basically right. Except that you wont find the number 987. Can you see why not?
You could fine-tune and optimise it a bit, but it will still look much the same.

Yeah I realized it! It should be <=. Thanks for the help James.

Thanks for the help James.

My pleasure! Strictly for your own info, here's a fully-optimised version (also counts how many solutions were found)

``````int n = 0;
for (int i = 2; i <= 9; i++) {
for (int j = 1; j < i; j++) {
for (int k = 0; k < j; k++) {
System.out.println(++n + "=" + i + j + k);
}
}
}``````
``````import java.util.*;

public class RandomTestingTest {
public static void main(String[]args){

Random noob=new Random();

int n1=noob.nextInt(10);
int n2=noob.nextInt(10);
int n3=noob.nextInt(10);

System.out.print(n1);
System.out.print(n2);
System.out.println(n3);
}
}
``````