# 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

Expert's answer

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

}

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;

}

## Comments

## Leave a comment