Write a program that outputs the total of 5-digit numbers with a five in them, but not with an 8

My challenge is to output the total number of five-digit numbers that have a digit 5, but no digits 8. My only two answers so far have been 0, or 90000. Can anyone help me? (Yes, I know the answer, but I'd like to know how to use if/else statements to achieve said answer, which is 26281)
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
  #include <iostream>
using namespace std;

int main() {
    int number;
    int counter=10000;
    int ncounter=0;
    while (counter >= 10000 && counter <= 99999) {
        int n1,n2,n3,n4,n5;
        counter = counter + 1;
        n1 = number%10;
        number /= 10;
        n2 = number%10;
        number /= 10;
        n3 = number%10;
        number /= 10;
        n4 = number%10;
        number /=10;
        n5 = number%10;
        number /= 10;
        if (n1 == 5||n2 == 5||n3 == 5||n4 == 5||n5 == 5)
            if (n1!=8)
                if (n2!=8)
                    if (n3!=8)
                        if(n4!=8)
                            if (n5!=8)
                                ncounter=ncounter+1;
    }
    cout<<ncounter<<endl;
    return 0;
}
Last edited on
1
2
n1 = number%10;
//... 
What is number? What does it equals to?
If zero is allowed as the leading digit of the five digit number (ie. 00001 is a five digit number),
number of five-digit numbers without 8 == 9^5 == 59049
number of five-digit numbers without 8, and without 5 == 8^5 == 32768
number of five-digit numbers without 8, and with 5 == 59049 - 32768 == 26281

Otherwise ( 00001 is not a five digit number),
number of five-digit numbers without 8 == 8 * 9^4 == 52488
number of five-digit numbers without 8, and without 5 == 7 * 8^4 == 28672
number of five-digit numbers without 8, and with 5 == 52488 - 28672== 26281 == 23816
Topic archived. No new replies allowed.