Tuesday, 17 June 2014

C++ program to find the sum of even numbers between 1 and n



Here is a C++ program to find the sum of even numbers between 1 and n


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
    long num,sum=2;
    cout<<"enter the number ";
    cin>>num;
    cout<<"2";
    for(int i=3;i<=num;i++)
    {
        if(i%2==0)
        {
          cout<<"+"<<i;
          sum+=i;
        }
    }
    cout<<" = "<<sum;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment