For Loop-Odd Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • XiCookieX
    New Member
    • Dec 2007
    • 7

    For Loop-Odd Numbers

    Well, I'm tryin to make a for loop that computes the sum of the odd numbers in the range from 0 to 100.
    So far i have
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()

    {
    int odd = 1;
    }
    i need a FOR (something,some thing,something )
    and an IF statement.
    and then i dont know what to do for the rest of the part... =(
    i know if you add like all the odd numbers from 1 to 100 it would equal 3000.
    how would i just take the odd numbers and add them all together using a for loop?

    Please Reply If you know the answer to my question
  • looker
    New Member
    • Dec 2007
    • 18

    #2
    Originally posted by XiCookieX
    Well, I'm tryin to make a for loop that computes the sum of the odd numbers in the range from 0 to 100.
    So far i have
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()

    {
    int odd = 1;
    }
    i need a FOR (something,some thing,something )
    and an IF statement.
    and then i dont know what to do for the rest of the part... =(
    i know if you add like all the odd numbers from 1 to 100 it would equal 3000.
    how would i just take the odd numbers and add them all together using a for loop?

    Please Reply If you know the answer to my question
    The algorithm is simple:
    First of all, you need to know what is the difference between
    - even number ( 2, 4, 6, 8...etc )
    - odd number ( 1, 3, 5, 7....etc )
    {2, 4, 6, 8...etc} divided by 2. The rest number will be Zero
    {1, 3, 5, 7....etc} divided by 2. The rest number will be One
    The operator you can use to calculate the the rest number from division is modulo ( % ).
    Example:

    4 % 2 = 0 : because the rest number is Zero ( even number )
    2 % 3 = 2 : because 2 is the rest number after we 2/3
    3 % 2 = 1 : odd number

    - Go looping through 0 to 100 ( as what is instructed in your requirement )
    - In each instance of your loop, test if it is odd/ even ( use modulo above )

    Hope this will help!!

    /looker

    Comment

    • dav3
      New Member
      • Nov 2006
      • 94

      #3
      Originally posted by XiCookieX
      Well, I'm tryin to make a for loop that computes the sum of the odd numbers in the range from 0 to 100.
      So far i have
      #include <iostream>
      #include <iomanip>
      using namespace std;
      int main()

      {
      int odd = 1;
      }
      i need a FOR (something,some thing,something )
      and an IF statement.
      and then i dont know what to do for the rest of the part... =(
      i know if you add like all the odd numbers from 1 to 100 it would equal 3000.
      how would i just take the odd numbers and add them all together using a for loop?

      Please Reply If you know the answer to my question
      in your for loop the first "something" is an integer (you want to set it to = 0). Your next is how long you want the for loop to run. So you want to use the variable you just created in the first statement to be < or > than something (hint: x <= 100). Your last stipulation is used to increment or decrement the variable.

      Inside your for loop you want to use modulo division (% operator) to see if the number is odd or not, if it is odd add it to a variable (call it sum), if it is even do nothing.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You could also make your for...loop start at the first odd number in the range, and then increment by the proper amount so that you never get an even number (hint: What's 3-1? 5-3? 7-5? The difference between two consecutive odd numbers is always...?)

        Comment

        • looker
          New Member
          • Dec 2007
          • 18

          #5
          Originally posted by XiCookieX
          Well, I'm tryin to make a for loop that computes the sum of the odd numbers in the range from 0 to 100.
          So far i have
          #include <iostream>
          #include <iomanip>
          using namespace std;
          int main()

          {
          int odd = 1;
          }
          i need a FOR (something,some thing,something )
          and an IF statement.
          and then i dont know what to do for the rest of the part... =(
          i know if you add like all the odd numbers from 1 to 100 it would equal 3000.
          how would i just take the odd numbers and add them all together using a for loop?

          Please Reply If you know the answer to my question
          Well this is the new algorithm, you can go with
          <Code removed - please read our Posting Guidelines>

          Comment

          • XiCookieX
            New Member
            • Dec 2007
            • 7

            #6
            okay i understand a bit
            i have this now
            {
            for (i=0;i<=100;i++ )
            if ((i%2)!=0)//So now it would only show the remainder of 2's
            }
            how would i get the odd intergers and add them together in a calculation after the if statement

            Comment

            • XiCookieX
              New Member
              • Dec 2007
              • 7

              #7
              Umm, umm
              well i dont know the sum of odd numbers from 0-100;
              i know that even numbers sum from 0-100 is 2550.
              can someone find me the sum of the even intergers between 0-10
              2+4+6+8+10+12+1 4+...etc...

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Originally posted by XiCookieX
                okay i understand a bit
                i have this now
                {
                for (i=0;i<=100;i++ )
                if ((i%2)!=0)//So now it would only show the remainder of 2's
                }
                how would i get the odd intergers and add them together in a calculation after the if statement
                OK, that if statement will correctly filter out every even number. So if the if statement is executed, i is an odd number. What do you want to do with that odd number? Add it to a sum. That sum has to be made outside the loop, as you want to see its value after the loop.

                Comment

                • XiCookieX
                  New Member
                  • Dec 2007
                  • 7

                  #9
                  no the if statement "if ((i%2)!=0)" would only take the numbers that has remainer of 2.
                  Now I figured it out, thx for your help guys =)
                  what i add after the if statement is
                  if ((i%2)!=0)
                  final=(i-1)+final

                  now the "i" is i++ so its going up by 1 until it hits 100
                  the calculation would only work if the number is remainer by 2, and then
                  taking that number and subtracting 1 from it. Making "i" an odd numbers.
                  now the interger final has no value its just "final = 0";
                  The odd number then goes on to the "Final" then the loop goes on again and
                  again until it reaches 100 and adding all of the odd intergers.

                  Thanks for all the help =)

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    If you have a number that isn't divisible evenly by 2, and subtract 1, what kind of number do you get? Is that the kind of number you want? Note that the i++ statement is not completely executed until you get to the loop's closing brace, if it has one. If not, it executes after the statement in the loop body is run.

                    Comment

                    • XiCookieX
                      New Member
                      • Dec 2007
                      • 7

                      #11
                      no, the if statement "if (i%2)!=0)" means: if i has a remainder of 2 but no 0 it will go to the calculation:
                      Final=(i-1)+final
                      since the i will always be even taking 1 away from it makes it odd interger
                      then those odd intergers get added on the to final which has a value of 0

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        Originally posted by XiCookieX
                        no, the if statement "if (i%2)!=0)" means: if i has a remainder of 2 but no 0 it will go to the calculation:
                        Final=(i-1)+final
                        since the i will always be even taking 1 away from it makes it odd interger
                        then those odd intergers get added on the to final which has a value of 0
                        Actually, no, Laharl is correct. Think about what the % operator does. It divides the left hand side by the right hand side, but takes the remainder. Now, this implies that the only possible answers lie between 0 and the right hand side minus 1. For example, let's look at x % 4 for several values of x:

                        Code:
                        x  |x / 4|x % 4
                        ---+-----+-----
                        0  |  0  |  0
                        1  |  0  |  1
                        2  |  0  |  2
                        3  |  0  |  3
                        4  |  1  |  0
                        5  |  1  |  1
                        6  |  1  |  2
                        7  |  1  |  3
                        8  |  2  |  0
                        9  |  2  |  1
                        If x % 4 ever resulted in 4, that would mean there was another 4 that could have been divided out, which means the remainder can't be 4, which contradicts our statement. Thus, x % y can only result in [0, y-1].

                        So x % 2 only results in 0 or 1, never 2. Now, what numbers are evenly divisible by 2 (meaning they have no remainder i.e. x % 2 == 0)? What numbers have a remainder of 1 when divided by 2 (i.e. x % 2 != 0)?

                        Comment

                        • dav3
                          New Member
                          • Nov 2006
                          • 94

                          #13
                          Originally posted by Ganon11
                          Actually, no, Laharl is correct. Think about what the % operator does. It divides the left hand side by the right hand side, but takes the remainder. Now, this implies that the only possible answers lie between 0 and the right hand side minus 1. For example, let's look at x % 4 for several values of x:

                          Code:
                          x  |x / 4|x % 4
                          ---+-----+-----
                          0  |  0  |  0
                          1  |  0  |  1
                          2  |  0  |  2
                          3  |  0  |  3
                          4  |  1  |  0
                          5  |  1  |  1
                          6  |  1  |  2
                          7  |  1  |  3
                          8  |  2  |  0
                          9  |  2  |  1
                          If x % 4 ever resulted in 4, that would mean there was another 4 that could have been divided out, which means the remainder can't be 4, which contradicts our statement. Thus, x % y can only result in [0, y-1].

                          So x % 2 only results in 0 or 1, never 2. Now, what numbers are evenly divisible by 2 (meaning they have no remainder i.e. x % 2 == 0)? What numbers have a remainder of 1 when divided by 2 (i.e. x % 2 != 0)?

                          That is a great description of modulo division. Nicely done sir.

                          Comment

                          • alfons
                            New Member
                            • Dec 2007
                            • 6

                            #14
                            using mod and all is way to complicated for this.
                            We just need a increment of +2 coz there's a difference of 2 between every consecutive odd no.

                            int a,b=0;
                            for(a=1;a<100;a +=2)
                            {

                            Comment

                            Working...