Answer to Question #15275 in C++ for Bill Jovan

Question #15275
program that will accept a number n and display the sum of all even numbers and the sum of all odd numbers from 1-n
1
Expert's answer
2012-09-25T11:32:57-0400
#include <iostream>
using namespace std;
int main (){

int n;

cout<<"Enter number n please!\n";

cin>> n ;

int sumEven, sumOdd;
sumOdd = sumEven = 0;
for (int i = 0 ; i <= n ; i++){
& if (i%2 == 0) sumEven+=i;
& else sumOdd+=i;
}
cout<< "Sum even number from 1 to n: "<<sumEven<<endl;
cout<< "Sum odd number from 1 to n: "<<sumOdd<<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
New on Blog
APPROVED BY CLIENTS