Solving C++ Problems: Finding Numbers Divisible by 5 or 7

  • Context: Comp Sci 
  • Thread starter Thread starter ZincPony
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
11 replies · 3K views
ZincPony
Messages
21
Reaction score
0
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 don't know where to start off with this one. any point in direction would be greatly appreciated.
 
Physics news on Phys.org
i know what the modulus operator is. its basically

do 500%5 = 0 while i++ but I am just stumped on the build of it. i know what its needed to work
 
Last edited:
How about something like this:

do
{
test if number is divisible by 5 OR 7 (modulus ?)
if yes, inc your count

inc number
} while (your exit condition)
 
ZincPony said:
i know what the modulus operator is. its basically

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

++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.
 
but i got to Use at least 1 increment operator and 1 decrement operator and 1 compound boolean expression.

soo i got to set i=500 then go if --i%5<1 then count++
 
Last edited:
but i also got to decrest 500 to a lesser number 500, 499, 498, 497. i got to use the i-- to soem extent
 
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).
 
this is what i could come up with from Rangers little write out
 
Last edited:
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.

Check your stopping condition:

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!
 
sweet. got it to work finally. thanks man appreciate it. :)
Code:
	  	{ 	
            iCount++;

		}
          iNum--;
		 }while (iNum>=0);
 
Last edited: