Tuesday, 17 June 2014

C++ program to find the sum of odd and even numbers separately



Here is a C++ program to find the sum of odd and even numbers separately


 SOURCE CODE OUTPUT
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int n,osum=0,esum=0;
    cout<<"Enter the number ";
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        if(i%2==0)
            esum+=i;
        else
            osum+=i;
    }
    cout<<"Sum of odd numbers = "<<osum<<endl;
    cout<<"Sum of even numbers = "<<esum;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment