Howdy,
I've been having problems letting the computer figure out my answer to my math riddle. Im given a multiplication problem to find the answer to the following problem:
7xx
x 4x
_____
xxxx0

In this problem, every number from 0-9 is used only once. I found the answer the hard way, but i know there is a much much more easier way of doing so. Can anyone help me? Here is my code. Thanks

public class Riddle
{
        public Riddle()
    {
     
        int i,
            j;
        
        for (i = 701; i < 799 && i > 700; i++)
        {
            //System.out.println(i);
            for(j = 40; j > 39 && j < 50; j++)
            {
                System.out.println(i + " \n" + "x" + j + "\n_______\n" + i*j + "\n");
            }
               
        }
    }
//715
//x46=
//32890
   
}

You better work harder on your school assignment as this may compile but will never run.
Next, there are some design flows. You have static values. What happens when you decide to use different values? You will need to re-write large portion of your code. Therefore introducing a way to read in values would be handy. Based on that hard coded values in for loops need to be replaced by variables (also please correct your logic of for loops). You need to come up with way to work out what will be the lowest possible value and maximum value for these variables. There is possibility of same result for more then one combination, you need to considerate that. Lastly, to print all combinations is pointless. Print only these that much result pattern.

commented: Lots for the OP to think about, though I rather suspect it's fallen on deaf ears. +29

Thanks for responding. I see where you're going but this program isnt made for user input. I'm a beginning programmer so sorry if my logic stinks. As far as printing out everything...thats where i need help. I only printed out everything so i could see all of the values. It took a good 10min to search through my terminal to find the answer lol.
My teacher gives hints as to something in the library that excludes a number if it is plugged in the problem. I've searched in the library and haven't found anything. Im just looking for a little nudge in the right direction

How about writing a function like this?

bool ContainsEachDigitOnce (int num1, int num2, int num3)
// returns true if each digit shows up once and exactly once
// in the three numbers, false otherwise.

Let the computer do the checking for you instead of you doing it. It's faster and more accurate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.