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++
Click For Summary

Discussion Overview

The discussion revolves around solving a programming problem in C++ that requires participants to find the total number of integers between 0 and 500 that are divisible by 5 or 7. The focus is on using a do…while loop, incorporating increment and decrement operators, and applying a compound boolean expression.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses confusion about how to start the problem and requests guidance.
  • Another suggests using the modulus operator to check for divisibility by 5 or 7.
  • Participants discuss the use of increment and decrement operators, clarifying the difference between preincrement and postincrement.
  • There are suggestions to structure the loop to increment a count when a number is divisible by 5 or 7.
  • One participant notes the requirement to use a decrement operator and proposes starting from 500 and decrementing.
  • Another participant advises against decrementing within the conditional statement and suggests adjusting the loop's stopping condition.
  • A participant reports successfully implementing a solution after considering the feedback received.

Areas of Agreement / Disagreement

Participants generally agree on the use of the modulus operator and the structure of the loop, but there are varying interpretations on how to correctly implement the increment and decrement operations, as well as the stopping condition for the loop. The discussion remains somewhat unresolved as different approaches are suggested without a definitive consensus on the best method.

Contextual Notes

Some participants express uncertainty about the specific requirements for using increment and decrement operators, and there are discussions about the correct stopping condition for the loop. These aspects may depend on individual interpretations of the problem statement.

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 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:
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:

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
19
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
21
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K