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
13 23623
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
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.
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...?)
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>
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
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...
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.
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 =)
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.
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
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: - 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)?
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: - 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.
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)
{
Sign in to post your reply or Sign up for a free account.
Similar topics
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
...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |