Finding a number 1- 9999

Sep 10, 2014 at 6:44pm
I need my code to start at 1 and go to 9999. If the code meets the first if-statement it'll go on. If not, than it will start over and add 1 on to the previous number tried. This is what I have now.

Try not to give me answers, just show me where i'm going wrong at as this is a homework assignment. Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <iostream>
using namespace std;

int main()
{
    int address = 0001, ones, tens, hundreds, thousands, sum = 29;
    
    while ( address < 9999)
        
    
    thousands = (address / 1000) % 10;
    hundreds = (address / 100) % 10;
    tens = (address / 10) % 10;
    ones = address % 10;
    
    
    if ( ones != tens && ones != hundreds && ones != thousands && tens != hundreds && tens != thousands && hundreds != thousands)
    {
        if ( address /2 % !0 )
        {
            if ( tens == (3 * thousands) )
            {
                if ( sum == ones + tens + hundreds + thousands )
                {
                    cout << "The address is: " << address << endl;
                }
                else
                {
                    address++;
                }
            }
            else
            {
                address++;
            }
        }
        else
        {
            address++;
        }
    }
    else
    {
        address++;
    }
    return 0;
    
    }
Sep 10, 2014 at 6:52pm
So what's the problem?

Aceix.
Sep 10, 2014 at 6:59pm
I need it to output the number that fits the conditions. Forgot to post those, whoops. They are :

1. All four digits are different

2. The digit in the thousands place is three times the digit in the tens place

3. The number is odd

4. The sum of the digits is 27.
Sep 10, 2014 at 7:56pm
Could anyone help me out?
Sep 10, 2014 at 8:09pm
Yes, first thing is that you need to make sure that everything below the while loop is executed at every iteration of the loop. As of now only line 11 is being executed and nothing else.

Next you need to make sure you are computing the right sum, your code and your post say 2 different things

Finally using a for-loop will make your code more compact so that the unnecessary else address++ is done only once

P.S. 0001 == 1
Topic archived. No new replies allowed.