Answer to Question #15342 in C++ for reymark

Question #15342
write a program that will accept a number n and display the sum of all even numbers and the sum of all odd numbers from 1 to n.
1
Expert's answer
2012-09-27T12:20:34-0400
#include<iostream.h>

bool even(int n){
if (n%2==0) return true; else return 0;
}

int sumOdd=0, sumEven=0;

void main(){
int m;
cout<<"Enter an integer: ";
cin>>m;
for (int i=0; i<=m; i++)
& if (even(i)) sumEven += i;
& else sumOdd += i;
cout<<"Sum of even numbers: "<<sumEven<<"\nSum of odd numbers: "<<sumOdd<<"\n";
}

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
APPROVED BY CLIENTS