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);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.