Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need some quick help. I want to count all the 5\'s in a range of #\'s. For examp

ID: 3630139 • Letter: N

Question

Need some quick help. I want to count all the 5's in a range of #'s. For example: 120-150. There are 6 fives in that range.

Here's what I have so far:

public class five {
    public static void main(String[] args) {
        int five=0;
        System.out.print("Please enter a starting value");
        int i;
        i = IO.readInt();
        System.out.print("Please enter an ending value");
        int z;
        z = IO.readInt();
       
        for ( i=0; i< z;i++);
        {if (i % 10 ==5)
            fivecount++;
       
        }
        System.out.print(fivecount);
   Any help would be appreciated

Explanation / Answer

I'm not sure what IO.readInt() is, so let me rewrite the main method. It's quite short anyways.

public static void main(String[] args)
{
// set up keyboard input
Scanner kb = new Scanner(System.in);

System.out.print("Please enter a starting value");
int start = kb.nextInt();

System.out.print("Please enter an ending value");
int end = kb.nextInt();

// store 5 count here
int fivecount = 0;

// loop from start to end, counting 5s
for(int i = start; i < end; i++)
if(i % 5 == 0)
fivecount++;

System.out.println(fivecount);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote