Help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xeric
    New Member
    • Nov 2006
    • 3

    #1

    Help!

    Hey guys. I'm sure these two programs are really simple, but I'm in my first java programming class and I don't have any programming background. I think I missed the class when we went over this stuff. Anyways, I have to write two programs for homework.

    1) Write a program that outputs the product of all the integers between 3 and 7, inclusive, that is, 3*4*5*6*7. Use while loop for this program.

    2) Write a program that counts how many multiples of 7 are between 33 and 97, inclusive. Use for loop for this program.

    I know how to use the for and while loops and everything, but I'm not sure how to go about writing these programs. Any help would be greatly appreciated. Thanks!
  • LacrosseB0ss
    New Member
    • Oct 2006
    • 112

    #2
    if you use a for loop for the first one it's very simple. Say for x=3 to x<8. Then you can use x inside the loop. total = total*x.

    for the second, again use a for loop starting @ 33 and go until 97. check each numebr using mod. If mod = 0, it's a mutiple. Use a counter if you need to output the number at the end.

    Comment

    • Xeric
      New Member
      • Nov 2006
      • 3

      #3
      Well, for the first one it HAS to be a while loop.. that's the assignment. Also, I don't exactly follow what you're saying on the other one either. I'm very new to this. What is mod?

      Comment

      • Xeric
        New Member
        • Nov 2006
        • 3

        #4
        This is what she told me when I emailed to ask about it.

        1)Finding the product of numbers between 3 and 7, is similar
        to finding the sum of n numbers program that we did in
        class.

        For this program you are supposed to use a while loop. First
        you need to identify the condition that will make the while
        loop execute. After that apply the logic similar to one that
        we did toady in class( instead of sum its going to be
        product , so you need to multiple).


        2) For the second program you are supposed to use a for
        loop, create a loop that iterates through all numbers
        between 33 and 97 . Then for each number check if it is
        divisible by 7. if true update the 'count of numbers
        divisible by 7'. Otherwise just continue with the loop.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          All right, break it down, just like your teacher said. For the first problem, your first step will be identifying the end condition. Well, you want to calculate numbers between 3 and 7, so it makes sense that you want to start at 3. Start an integer variable (index) and set it to 3 before your loop. If you know you're going to be increasing this variable each time, then you want it to stop when it's larger than 7. In other words, you only want your while loop to execute while the variable is less than 8.

          Now, you're multiplying and storing this value into a variable, so you should start another integer variable (sum) before your loop and set it to 1. This way, when your loop first executes, you can say sum = sum * index; and will get 3 to start.

          Finally, inside your loop, all you will be doing is two things:

          1) Update sum by multiplying it by index
          2) Update index by adding 1

          Good luck!

          Comment

          • LacrosseB0ss
            New Member
            • Oct 2006
            • 112

            #6
            My apologies xeric. Mod is a mathematical operation which returns the remainder of a division problem. The syntax for this is # % # and the answer will be the remainder.

            Example:
            4 % 2 = 0
            5 % 2 = 1
            etc

            In your case you would use # (depending where in the loop you are) % (mod) 7 and check if it is 0. If it is, that number is a factor of 7. Hopefully this helps.

            Comment

            Working...