473,387 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.