Hello,
I am trying to write a program with while loops to add up a number of even/odd numbers. I have figured out everything except how to get the program to add up only the even numbers between two numbers in the middle of the numbers set. For instance, say a number set is 1-2500, I can get it to add up all the odd numbers by using a switch statement, but I cannot get it to add up only the even numbers from say 350-692 in the same program. I am very frustrated and have been working on it for hours tearing it down and reworking it time and time again with smaller numbers to no avail. Could someone please help me, I'm desperate and very frustrated?
Here is what I have:
[/#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>
What you have works pretty well you can simplify it by just doing this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
while (number <= 2582) //you can modify your range here
{
switch (number % 2)
{
case 0: //Even Number
//count here
//add sum here
break;
case 1: //Odd Number
//count here
//add sum here
break;
}
number++;
}
How do I get the the program to ad up just the even numbers from the middle is what I don't understand. When I compile what I had it doesn't do it right. It works fine if I just want to add up the evens and the odds, but that's not what I'm tring to do. I need to add up all the odds, and then just the evens from the middle of the same block of numbers.
What do you mean by middle? Can you give an example because I don't see why you cant just modify "number" and 2582 to get it to do what you want it to do.
I'm trying to add all of the odd numbers from 1 to 3579, and even numbers from 522-2222 in the same program. That's why I had an if/else in the switch statement, but it didn't work when I compiled it. I worked it with a much lower set of numbers so I knew what to expect if it worked right. For instance, if you add odds from 1-10 it comes our to 30, and if you add only evens from 4 to 8, omitting 2 and 10 it comes out to 16.
Thanks for your help, I've been working on it for hours. I'm so new at this, it seems like it takes forever just to get a small program done right.
I just tried it with the && and it worked on the lower numbers- my bad, the sum was 18. Than t you so very very much-I feeel like an idot trying to learn this stuff
//Cplusplus.com challenge ;)
//man hope this helps!
//jdstufu@yahoo.com
//programmed this using codeBlocks IDE with MINGW 3.4.2 compiler
#include <iostream>
#define n 3579
#define lowBound 522
#define upBound 2222
using namespace std;
int main()
{
unsigned short i, oddCount = 0, oddSum = 0, evenCount = 0, evenSum = 0;
for(i=0; i<n; i++)
{
if(i%2==1)//check if the number is odd
{
oddCount++; //increment the odd counter since the number is odd
oddSum += i; //add the number to the oddSum since its odd
}
if((i>=lowBound)&&(i<=upBound)) // check if 522 <= n <= 2222
{
if(i%2==0)//check if the number is even
{
evenCount++; //increment the even counter since the number is odd
evenSum += i; //add the number to the evenSum since its odd
}
}
}
cout << "Total even numbers added: " << evenCount << "\n";
cout << "Total odd numbers added: " << oddCount << "\n";
cout << "Summation of all even numbers: " << evenSum << "\n";
cout << "Summation of all odd numbers: " << oddSum << "\n";
return 0;
}
//Cplusplus.com challenge ;)
//man hope this helps!
//jdstufu@yahoo.com
//programmed this using codeBlocks IDE with MINGW 3.4.2 compiler
#include <iostream>
#define n 3579
#define lowBound 522
#define upBound 2222
usingnamespace std;
int main()
{
unsignedshort i, oddCount = 0, oddSum = 0, evenCount = 0, evenSum = 0;
for(i=0; i<n; i++)
{
if(i%2==1)//check if the number is odd
{
oddCount++; //increment the odd counter since the number is odd
oddSum += i; //add the number to the oddSum since its odd
}
if((i>=lowBound)&&(i<=upBound)) // check if 522 <= n <= 2222
{
if(i%2==0)//check if the number is even
{
evenCount++; //increment the even counter since the number is odd
evenSum += i; //add the number to the evenSum since its odd
}
}
}
cout << "Total even numbers added: " << evenCount << "\n";
cout << "Total odd numbers added: " << oddCount << "\n";
cout << "Summation of all even numbers: " << evenSum << "\n";
cout << "Summation of all odd numbers: " << oddSum << "\n";
return 0;
}