An odd number divided by 2 always produces a remainder equal to 1.
An even number divided by 2 always produces a remainder equal to 0.
To get the remainder of a number we will use the modulo operator %
So I will write a function that will retrun the sum of even numbers.
1 2 3 4 5 6 7 8 9 10 11
int EvenSum(int n)
{
int EvenSum=0; // initializing the sum with 0
for(int i=0; i<=n; i++)
if (i%2==0) // if i is even
{
EvenSum+=i; //every time i is even we add i to the sum
}
return EvenSum;
}
In main we will call the function
1 2 3 4 5 6 7 8 9 10
int main()
{
int n;
cout<<"n=";
cin>>n;
cout<<EvenSum(n); // calling the function
_getch();
}
Using the same idea you can do a function for Odd Numbers.
If you have any questions feel free to answer.
This is a simple factorial program. It doesn't matter what compiler or IDE you're using. bubba89 already gave you the answer. Have you even tried to compile it or are you just waiting for someone to do ALL the work for you?