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

  • Thread starter Thread starter ZincPony
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion focuses on using a do...while loop in C++ to count numbers between 0 and 500 that are divisible by 5 or 7. Participants emphasize the importance of the modulus operator for checking divisibility and suggest using both increment and decrement operators effectively. A key point is to ensure the loop continues until the number reaches zero, correcting the stopping condition to "while (iNum >= 0)". The final solution involves incrementing the count when a number meets the divisibility condition and decrementing the number for each iteration. The collaborative effort leads to a working solution that meets the specified requirements.
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
Have you ever used the modulus operator? That could be helpful.
 
i know what the modulus operator is. its basicly

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 basicly

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:
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
 
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).
 
  • #10
this is what i could come up with from Rangers little write out
 
Last edited:
  • #11
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!
 
  • #12
sweet. got it to work finally. thanks man appreciate it. :)
Code:
	  	{ 	
            iCount++;

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