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.

Expert's answer

#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";
}
LATEST TUTORIALS
APPROVED BY CLIENTS