Question: Write ( define ) a public static method named countDivisible 3 or 5 , that takes two int arguments and returns an int value. You

Write (define) a public static method named countDivisible3or5, that takes two int arguments and returns an int value. You can safely assume that the second argument value will be greater than the first argument value. When this method is called, it should count all of the values between the first argument value and the last argument value (inclusive) that are evenly divisible by either 3 or 5. Your method should return this count.
Examples:
countDivisible3or5(1,20) will return 9 because there are 9 numbers between 1 and 20 that are evenly divisible by either 3 or 5. These numbers are (3,5,6,9,10,12,15,18,20) countDivisible3or5(3,6) will return 3 because there are 3 numbers between 3 and 6 that are evenly divisible by either 3 or 5. These numbers are (3,5,6) countDivisible3or5(1,2) will return 0 because there are no numbers between 1 and 2 that are evenly divisible by either 3 or 5.
You may wish to write some additional code to test your method
Helpful Hints:
Use a counter controlled while loop to iterate from the first argument value to the second argument value. As you iterate, use an if statement to test whether or not each value is either evenly divisible by 3 or 5. Keep track of the count of how many numbers between the first argument value to the second argument value are evenly divisible by either 3 or 5. Return that count when the loop is finished.
( a Java for loop would also be a reasonable choice here.)
Remember that you can use the modulus operator (%) to compute the reminder of an integer division operation.
For example: : 15%3 is 0, because 3 goes into 15 evenly with no remainder. On the other hand, 13%5 is 3, because 5 goes into 13 two times with 3 left over.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!