just do interger division
if (number%2 %26gt; 0)
{
odd = odd + number;
}
else
{
even = even + number;
}
what % does is give you a remainder when doing interger division, the number has to be of type int. so if the number divides evenly by 2 then the remainder is 0. so we know it's even, if you divide 3 by two the % will equal 1, so we know the number is odd, etc... hmm might not work for 1, so make if read:
if (number%2 %26gt; 0 || number == 1)
so cut and paste this into a compiler:
#include %26lt;iostream%26gt;
int main()
{
using namespace std;
int number, even(0), odd(0);
do
{
cout %26lt;%26lt; "Please enter a number %26lt;Enter 0 to end input%26gt;: ";
cin %26gt;%26gt; number;
if (number%2 %26gt; 0 || number ==1)
odd = odd + number;
else
even = even + number;
}while (number != 0);
cout %26lt;%26lt; "The sum of the odd numbers entered is " %26lt;%26lt; odd %26lt;%26lt; endl;
cout %26lt;%26lt; "The sum of the even numbers entered is " %26lt;%26lt; even %26lt;%26lt; endl;
system("pause");
return 0;
}
that was kinda of fun, enjoy (won't work for negative, you can do that part). you call always change this to read from a file as well, the rest is up to you, i already gave you too much.
How to write a program in C++ that reads a set of integers and finds and prints the sum of the even and odd #.
Doing your homework, eh?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment