1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

More C++ Problems

  1. Jun 17, 2007 #1
    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. jcsd
  3. Jun 17, 2007 #2

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Have you ever used the modulus operator? That could be helpful.
     
  4. Jun 17, 2007 #3
    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
  5. Jun 17, 2007 #4

    ranger

    User Avatar
    Gold Member

    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)
     
  6. Jun 17, 2007 #5

    ranger

    User Avatar
    Gold Member

    ++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.
     
  7. Jun 17, 2007 #6
    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
  8. Jun 17, 2007 #7

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  9. Jun 17, 2007 #8
    but i also gotta decrest 500 to a lesser number 500, 499, 498, 497. i gotta use the i-- to soem extent
     
  10. Jun 17, 2007 #9

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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).
     
  11. Jun 18, 2007 #10
    this is what i could come up with from Rangers little write out
     
    Last edited: Jun 19, 2007
  12. Jun 18, 2007 #11

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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!
     
  13. Jun 18, 2007 #12
    sweet. got it to work finally. thanks man appreciate it. :)
    Code (Text):

            {  
                iCount++;

            }
              iNum--;
             }while (iNum>=0);
               
             
     
    Last edited: Jun 19, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: More C++ Problems
  1. C++ problems (Replies: 12)

  2. C++ Problem (Replies: 6)

  3. C++ problem (Replies: 3)

  4. Problem infile.get(c) (Replies: 1)

Loading...