add even (or odd) numbers from input

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2006
Posts: 3
Reputation: cynthann is an unknown quantity at this point 
Solved Threads: 0
cynthann's Avatar
cynthann cynthann is offline Offline
Newbie Poster

add even (or odd) numbers from input

 
0
  #1
Mar 4th, 2006
Hello
Please help. I am having a hard time making the leap from where I am to then picking out numbers (in this instance adding even numbers). Any suggestions or pointers would be greatly appreciated.

#include <iostream>
using namespace std;
// program computes the sum of all even numbers from input number

int compute_sum(int n);
int main()
{
int n;

cout << "enter a number" << endl;

cin >> n;

if (n % 2 == 0)
cout <<"even "<<compute_sum(n)<< endl;

else
cout << "odd " << endl;

return 0;
// return ((n & 1) == 1); // odd
// return ((n & 1) == 0); // even
}

int compute_sum(int n)
{
int count = 1;
int sum = 0;
while (count <= n)
{
sum = sum + count;
count = count + 1;
}
return sum;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: add even (or odd) numbers from input

 
0
  #2
Mar 5th, 2006
// program computes the sum of all even numbers from input number
This part of the objective does not make sense to me. Care giving a sample input number, like 12, and explaining what the expected result would be?
Right now it gives the result
  1. 1 + 2 + 3 + ... + 11 + 12 = 78
for the input of 12.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: cynthann is an unknown quantity at this point 
Solved Threads: 0
cynthann's Avatar
cynthann cynthann is offline Offline
Newbie Poster

Re: add even (or odd) numbers from input

 
0
  #3
Mar 5th, 2006
So, if you input 12 it would add

2 + 4 + 6 + 8 + 10 + 12
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: add even (or odd) numbers from input

 
0
  #4
Mar 5th, 2006
You can do that by just changing these lines.I am not giving you the answer because if you wrote the above program yourself, you can surely do this part using the hint I have given you.
  1. count = 1;
  2. count = count + 1;
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: cynthann is an unknown quantity at this point 
Solved Threads: 0
cynthann's Avatar
cynthann cynthann is offline Offline
Newbie Poster

Re: add even (or odd) numbers from input

 
0
  #5
Mar 5th, 2006
duh! Thank you for your help. You know when you have been sitting in front of something for so long that the screen begins to melt?

count = 2;
count = count + 2;
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: add even (or odd) numbers from input

 
0
  #6
Mar 5th, 2006
I can think of a better way. if you wish to calculate the sum of all numbers in a range, the formula is
n( (First+Last)/2 )

in your case, the first number is always going to be 2 ... 'n' is the "number of numbers" in the range.. eg, from 2-to-12, n=6 ... or 2-to-100, n=50... or 2-to-10000, n=5000.. (see a pattern there?)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: ruhneb03 is an unknown quantity at this point 
Solved Threads: 0
ruhneb03 ruhneb03 is offline Offline
Newbie Poster

Re: add even (or odd) numbers from input

 
0
  #7
Mar 5th, 2008
Hi cynthann,

Can you give me the complete working program of that if you get it right? Thanks.
Last edited by ruhneb03; Mar 5th, 2008 at 9:16 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 10478 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC