Just A Mathematician
Just A Mathematician

Reputation: 240

Problem trying to code a mathematical test which returns certain numbers

My issue seems simple, but I'm relatively new to programming and I cannot figure out what is wrong with my code.

I am supposed to return the number of numbers under n (max 1000000) that have both a 7 and a 9 in them. For example, n=10 returns 0, n=100 returns 2 (from 79 and 97) etc.

int allWith7Or9(int n) 
{
  int count = 0;
  for(int i = 0; i < n; i++)
  {
    if(i%10==7 || i%100==7 || i%1000==7 || i%10000==7 || i%100000==7 || i%1000000==7)
    {
      if(i%10==9 || i%100==9 || i%1000==9 || i%10000==9 || i%100000==9 || i%1000000==9)
      {
        count++;
      }
    }
  }
  return count;
}

I realize that I could have used &&, and I tried to, but my code keeps coming up with 0, and I have no idea why. I implemented this at least 3 different ways.

Here is another implementations I tried:

int allWith7Or9(int n) 
{
  int count = 0;
  for(int i = 0; i < n; i++)
  {
    if((i%10==7 || i%100==7 || i%1000==7 || i%10000==7 || i%100000==7
    || i%1000000==7) && (i%10==9 || i%100==9 || i%1000==9
    || i%10000==9 || i%100000==9 || i%1000000==9))
    {
      count++;
    }
  }
  return count;
}

Thank you!

Upvotes: 0

Views: 229

Answers (3)

Andy Turner
Andy Turner

Reputation: 140318

if(i%10==7 || i%100==7 || i%1000==7 || i%10000==7 || i%100000==7 || i%1000000==7)
{
  if(i%10==9 || i%100==9 || i%1000==9 || i%10000==9 || i%100000==9 || i%1000000==9)
  {
    count++;
  }
}

is basically

if(cond1)
{
  if(cond2)
  {
    count++;
  }
}

which is basically:

if(cond1 && cond2)
{
  count++;
}

since both conditions must be true to increment count.

And those conditions aren't true at the same time because

i % 100 == 7

isn't what you probably mean. This isn't checking for numbers like 70, 71 etc, it's checking for numbers what have a remainder 7 when divided by 100, like 7, 107, 207. So

i%10==7 || i%100==7 || i%1000==7 || i%10000==7 || i%100000==7 || i%1000000==7

is only matched when i == 7, and

i%10==9 || i%100==9 || i%1000==9 || i%10000==9 || i%100000==9 || i%1000000==9

is only matched when i == 9. 7 != 9, so the counter won't be incremented.


You can rewrite the conditions as:

i%10==7 || (i/10)%10==7 || (i/100)%10==7 etc

But writing it all out like this is messy and error-prone.

Instead, use a loop:

boolean found7 = false;
boolean found9 = false;
for (int ii = i; ii > 0: ii /= 10)  {
  switch (ii % 10) {
    case 7: found7 = true; break;
    case 9: found9 = true; break;
  }
}

if (found7 && found9) {
  count++;
}

This uses two flags to indicate which of the digits you have found. Check the last digit to see if it is 7 or 9, then divide by 10 to discard that digit and move on to the next.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718886

The fundamental problem is that your conditions are wrong.

For example, i % 100 == 7 does not test if the "tens" digit of i is 7. It actually tests if the last two digits are 07. Do the math ....

To test the "tens" digit is 7 in a single expression, you need to use something like

(i / 10) % 10 == 7

(You should be able to work out a correct solution from the above. And there are at least two different approaches that you could take.)

Upvotes: 0

Tom&#225;š Dejmek
Tom&#225;š Dejmek

Reputation: 151

Ok, first condition is wrong. For example 79. Condition for 7 does not works 79%10 = 9, 79%100 = 79. Seven is not detected. It should be something like iterate through all digits of number. So check for one number can be like this.

boolean have7and9(int a) {
  boolean seven = false, nine = false;  
  while(a > 0) {
    if (a%10 == 7) seven = true;
    if (a%10 == 9) nine = true;
    a /= 10;
  }
  return seven && nine;
}

Upvotes: 1

Related Questions