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).

Expert's answer

#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;

}

#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;

}

``

## Comments

## Leave a comment