# Homework Help: More C++ Problems

1. Jun 17, 2007

### ZincPony

1. 1. Use a do…while loop to determine the total number of numbers between 0 and 500 that are evenly divisible by 5 or 7.
Output should look like this:

The total number of numbers less than 500 that are divisible by either 5 or 7 is 157.

Use at least 1 increment operator and 1 decrement operator and 1 compound boolean expression.

2.I'm lost i dont know where to start off with this one. any point in direction would be greatly appreciated.

2. Jun 17, 2007

### Math Is Hard

Staff Emeritus
Have you ever used the modulus operator? That could be helpful.

3. Jun 17, 2007

### ZincPony

i know what the modulus operator is. its basicly

do 500%5 = 0 while i++ but im just stumped on the build of it. i know what its needed to work

Last edited: Jun 17, 2007
4. Jun 17, 2007

### ranger

do
{
test if number is divisible by 5 OR 7 (modulus ?)

inc number

5. Jun 17, 2007

### ranger

++i is preincrement and i++ is postincrement. In the former i is incremented before being used and in the latter i is incremented after being used.

6. Jun 17, 2007

### ZincPony

but i gotta Use at least 1 increment operator and 1 decrement operator and 1 compound boolean expression.

soo i gotta set i=500 then go if --i%5<1 then count++

Last edited: Jun 17, 2007
7. Jun 17, 2007

### Math Is Hard

Staff Emeritus
Each time you test a number, you'll want to increment your count if either of two cases is true:
if i%5==0 or if i%7==0

8. Jun 17, 2007

### ZincPony

but i also gotta decrest 500 to a lesser number 500, 499, 498, 497. i gotta use the i-- to soem extent

9. Jun 17, 2007

### Math Is Hard

Staff Emeritus
Look again at Ranger's suggestion and consider decrementing the number instead of incrementing it. You have two separate variables you are working with - one is the "count" that you increment only whenever the test condition is met, and the other is the actual number that you are testing which you could start at 500 and decrement for every new test (loop).

10. Jun 18, 2007

### ZincPony

this is what i could come up with from Rangers little write out

Last edited: Jun 19, 2007
11. Jun 18, 2007

### Math Is Hard

Staff Emeritus
You're getting close. Don't decrement iNum inside your "if" statement. Move that outside of it. The way it is now, iNum can't get decremented unless it has a value that is divisible by 5 or 7 and that is not always the case.

while (iNum==0);

iNum is 499 when you start. It gets decremented to 498 and then there's a check whether to run the loop again. Your instructions say only go through the loop again when iNum is zero, so that's a problem.

try

while (iNum>=0);

Hope this helps!

12. Jun 18, 2007

### ZincPony

sweet. got it to work finally. thanks man appreciate it. :)
Code (Text):

{
iCount++;

}
iNum--;
}while (iNum>=0);

Last edited: Jun 19, 2007