Home Why isn't my loop method working?
Reply: 3

Why isn't my loop method working?

Just A Mathematician
1#
Just A Mathematician Published in 2018-01-13 01:05:38Z

This may be a simple problem for many, but I cannot for the life of me 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.

EDIT:

I also tried this:

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;
}
Andy Turner
2#
Andy Turner Reply to 2018-01-13 10:21:03Z
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.

Tomáš Dejmek
3#
Tomáš Dejmek Reply to 2018-01-13 01:20:40Z

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;
}
Stephen C
4#
Stephen C Reply to 2018-01-13 01:54:22Z

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.)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321691 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO