Answer to Question #49211 in C++ for Joemar

Question #49211
with the use of switches, determine and print out the sum of all even and odd integers from 1 to 10 (Note: the sum of even integers for the given range would be equal to the sum of 2,4,6,8, and 10. For the odd integers, it will be the sum of 1,3,5,7 and 9).
1
Expert's answer
2014-11-21T00:53:12-0500
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int main()
{
int evenSum = 0;
int oddSum = 0;
for (int i = 0; i < 10; i++)
{
int temp;
temp = (i+1) % 2;
switch(temp)
{
case 1: oddSum += i + 1; break;
case 0: evenSum += i + 1; break;
}
}
cout<<"Sum of all even integers : "<<evenSum<<endl;
cout<<"Sum of all odd integers : "<<oddSum<<endl;
system("pause");
return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS