| | |
add even (or odd) numbers from input
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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;
}
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;
}
•
•
•
•
// program computes the sum of all even numbers from input number
Right now it gives the result
C++ Syntax (Toggle Plain Text)
1 + 2 + 3 + ... + 11 + 12 = 78
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.
C++ Syntax (Toggle Plain Text)
count = 1; count = count + 1;
I can think of a better way. if you wish to calculate the sum of all numbers in a range, the formula is
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?)
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?)
![]() |
Similar Threads
- Loop counting odd and even numbers (C++)
- Adding the sum of odd numbers. Please help (Visual Basic 4 / 5 / 6)
- print odd and even numbers (Assembly)
Other Threads in the C++ Forum
- Previous Thread: Binary Tree
- Next Thread: Hello, New CS Student has Question
Views: 10478 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






