0
C++
Write a C-program that finds and prints the sum of even and odd integers from 2 to 30 separately by using the switch selection.
2 odpowiedzi
0
why not try loops
0
I am not sure if this answers your question. This works...
#include <iostream>
using namespace std;
int main() {
int x,evenSum =0,oddSum=0;
for (x=2;x<=30;x++)
{
int i;
i=x%2;
switch(i)
{
case 0:
evenSum+=x;
continue;
case 1:
oddSum+=x;
continue ;
}
}
cout << "Sum of even numbers from 2 to 30 "<<evenSum<<endl;
cout << "Sum of odd numbers from 2 to 30 "<<oddSum<<endl;
return 0;
}