if input is 0 150 17 the last number is 17

i need program that find how many 17 in 0 to 150?

output should be 2

NOTE
the program should run for any 3 input number

Well, we simply can't create it for you (that will defeat the purpose of learning wouldn't it?) so what have you tried that we can assist you with?

this is my program
but it only read number from 1 to 100
how can i make this from 1 to 1000000

    System.out.print("Enter 1 number");
    Scanner input = new Scanner(System.in);
    int from = input.nextInt();
    System.out.print("Enter 2 number");
    int to = input.nextInt();
    System.out.print("Enter 3 number");
    int num3 = input.nextInt();

    int count = 0;
    for (int i = from; i <= to; i++) {
        int j = i;
        while (j > 0) {
            if (j % 10 == num3) {
                count++;
            }
            j /= 10;

        }
    }
    System.out.println(count);
}

Time to play computer!
Take a piece of paper and a pencil.
On top put i , j , Count , perhaps j%10 etc.
Now start looping and fill in line per line, the values of the variables or expressions.
Do this until i <= to or until you find an inconsistancy.
This method helped me out in a lot of situations like this. Success!

Execute the code as the compiler would but do it on paper....Or you can use netbeans and use the 'break' function to step line by line through your code and see how the variable changes/updates.

Edited 3 Years Ago by Phillamon

In your for loop you are setting i equal to from (following your example (0 150 17))
Then you are setting j equal to i , i being 0 (You should have written that on your paper!) While loop does not get executed. Next i becomes 1 and so is j write it down on your paper, Now the while loop gets executed, write down any change in variables etc. etc.

Pity you don't like to play computer on a piece of paper.
You would have found out that count never gets incremented if num3=17, because j % 10 always evaluates to a number between 0 and 9.

i know that already my problem is if i change 10 to 100 the program can't run the 0 to 9..
that is my problem..what should i do...

i need program that find how many 17 in 0 to 150?

Could you explain more clearly what you mean here?
Does it mean how many times 17 goes into 150?

yes. if my first input is 0 and second is 150 and last is 17
program should find how many 17 in 0 to 150
output should be 2
because of 17 and 117

i need the program that count any last input number fron 0 to 1000000

Hello the_yuo

You keep saying what you want. You are not prepared to try the excellent advice you have been given, so you do not understand how your existing code works. let alone understand how to change it.
Unless you are prepared to show some effort in trying to help yourself I'm going to close this thread.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Calculate the length of num3. In case of 17 this should give 2.
Calculate the power 10^2 this should give you 100.Let's call this n.
Now in your for loop do:
Throw away the while and the j and use:
if ( i % n == num3 ) then count++ and write i or whatever.
Success!

Edited 3 Years Ago by ddanbe: Listening to the moderator who is correct.

convert the number into string then use substring function to find the given number..and on every sucess increment the count..dont use % logic here..

Comments
Good idea! I just followed the OP strategy
This article has been dead for over six months. Start a new discussion instead.