468,463 Members | 2,036 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

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,something,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
Dec 5 '07 #1
13 23214
looker
18
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,something,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
Dec 6 '07 #2
dav3
94
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,something,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.
Dec 6 '07 #3
Ganon11
3,652 Expert 2GB
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...?)
Dec 6 '07 #4
looker
18
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,something,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>
Dec 6 '07 #5
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
Dec 6 '07 #6
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+14+...etc...
Dec 6 '07 #7
Ganon11
3,652 Expert 2GB
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.
Dec 6 '07 #8
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 =)
Dec 7 '07 #9
Laharl
849 Expert 512MB
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.
Dec 7 '07 #10
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
Dec 7 '07 #11
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. x  |x / 4|x % 4
  2. ---+-----+-----
  3. 0  |  0  |  0
  4. 1  |  0  |  1
  5. 2  |  0  |  2
  6. 3  |  0  |  3
  7. 4  |  1  |  0
  8. 5  |  1  |  1
  9. 6  |  1  |  2
  10. 7  |  1  |  3
  11. 8  |  2  |  0
  12. 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)?
Dec 7 '07 #12
dav3
94
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:

Expand|Select|Wrap|Line Numbers
  1. x  |x / 4|x % 4
  2. ---+-----+-----
  3. 0  |  0  |  0
  4. 1  |  0  |  1
  5. 2  |  0  |  2
  6. 3  |  0  |  3
  7. 4  |  1  |  0
  8. 5  |  1  |  1
  9. 6  |  1  |  2
  10. 7  |  1  |  3
  11. 8  |  2  |  0
  12. 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.
Dec 7 '07 #13
alfons
6
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)
{
Dec 24 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Charles Alexander | last post: by
43 posts views Thread by Gremlin | last post: by
5 posts views Thread by Martin Schou | last post: by
32 posts views Thread by Toby Newman | last post: by
2 posts views Thread by Alex | last post: by
3 posts views Thread by Ben R. | last post: by
32 posts views Thread by cj | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.