Monday, 16 June 2014

C++ program to find the sum of all odd numbers from 1 to n



Here is a C++  program to find the sum of all odd numbers from 1 to n


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

Any questions regarding to program please write in comments.

No comments:

Post a Comment