1

I'm trying to determine how many times the number four appears in a sequence of numbers. There is a lower bound and upper bound (inclusive) that the user can determine. My code so far only counts the number of times the number 4 occurs if the range is set 40-49-- the resulting output is 10, however the actual value should be 11 because 44 has two 4's. In addition, the output for a range of 1-10 should be 1 but instead I get a value of zero? Am I not properly checking for the occurrence of 4? I'm taking into account the difference places (one's, tenths, hundredths place).

Scanner scan = new Scanner(System.in);
    int count = 0;
    int i;

    System.out.println("Enter the lower range: ");
    int lower = scan.nextInt();

    System.out.println("Enter the upper range: ");
    int upper = scan.nextInt();

    if (lower > upper) {
        System.out.println("Bad input");
    }
    for (i = lower; i <= upper; i++) {
        if (lower * 0.01 == 4) {
            count++;
        } else if (lower * .1 == 4) {
            count++;
        } else if (lower * 1 == 4) {
            count++;
        }
    }

    System.out.println("Result: " + count);
  • 1
    I don't see any arrays here – UnholySheep Oct 23 '16 at 18:26
  • There isn't supposed to be. The user determines the lower and upper limit and from that you have determine the occurrence of the number 4. – Construct0r Oct 23 '16 at 18:28
  • Possible duplicate of [How to get the separate digits of an int number?](http://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number) –  Oct 23 '16 at 18:28
  • So if the user entered, lower: 0 upper: 10 (0 1 2 3 4 5 6 7 8 9 10) the number 4 shows up once. But, that doesn't work out for my code. – Construct0r Oct 23 '16 at 18:28
  • The way can be found out, just think with modulo and division. Maybe in addition you need to know amount of digits. However, I don't think that it is useless question so that I have given +1 – Soner from The Ottoman Empire Oct 23 '16 at 18:30
  • 0 time anything will always be 0, not sure what else you expected. (You might want to rethink your entire `for` loop) – UnholySheep Oct 23 '16 at 18:30
  • what do you want to achieve? check every number iteratively? that's naive. Think of a better algo first, mate (hint: AFAIR, you can calculate that without a loop, with just a proper equation) –  Oct 23 '16 at 18:31
  • [also, you already asked this question.](http://stackoverflow.com/questions/40113717/what-would-the-condition-be) –  Oct 23 '16 at 18:33

2 Answers2

1

Don't use floating point math. The results are subject to accuracy errors. Stick to integer arithmetic.

Hint: Use modulus % and division / to pull out particular digits.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • What is I did some casting to an int before doing the floating point math would that still produce accuracy errors? Also, can using a combination of modulus(%) and division(/) be used for any numbers? Say a number like 454 or 96544? – Construct0r Oct 23 '16 at 18:35
1

Below Java Code will solve your problem:

try (Scanner scan = new Scanner(System.in)) {
            int count = 0;
            int i;

            System.out.println("Enter the lower range: ");
            int lower = scan.nextInt();

            System.out.println("Enter the upper range: ");
            int upper = scan.nextInt();

            if (lower > upper) {
                System.out.println("Bad input");
            } else {
                int num=0;
                for (i = lower; i <= upper; i++) {
                    num=i;
                    while(num>0){
                        if(num%10==4){
                            count++;
                        }
                        num=num/10;
                    }
                }
                System.out.println("Result: " + count);
            }
        }

Note: When IF execute it not means below code will not execute. just write else part or exit program.

Ramesh
  • 200
  • 1
  • 2
  • 13
  • What is the purpose of num = num/10 in the while loop? How can this change if negative value are used? – Construct0r Oct 23 '16 at 19:02
  • If the values were negative, would you just set the condition in the while loop to be (num < 0)? – Construct0r Oct 23 '16 at 19:03
  • For negative numbers code will change. Here only positive numbers test case are passed using above code. – Ramesh Oct 23 '16 at 19:15
  • Okay I think I got it. While(num > 0) check if (num % 10 == 4) if that is false then keep dividing number by and check to see if the remainder is 4. If the remainder is 4 increment the count by one? – Construct0r Oct 23 '16 at 19:18
  • Yes. For negative numbers test cases simple logic consider most negative number as upper number & least negative number as lower number & same code is used to pass negative cases as. – Ramesh Oct 23 '16 at 19:25
  • 1
    This code doesn't work for a lower bound that is negative. E.g. lower = -4, upper = 4 results in a count of 1 instead of 2. It's not entirely clear from the question whether this is correct behavior or not, but seems like it would be incorrect based on what we know of the requirements. To fix this change `num = i;` to `num = Math.abs(i);` – D.B. Oct 24 '16 at 02:27
  • As already mentioned above code covers only positive number test cases. Yes to work with negative number your suggestion is correct. Thanks! – Ramesh Oct 24 '16 at 04:27