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;
Comments
Leave a comment