4/01/2009

C++ Program To Calculate The Sum Of All Even And Odd Numbers Up to N


/* C++ Program To Calculate The Sum Of All Even And Odd Numbers Up to N */

#include <iostream>

using namespace std;

int main()
{
    int i=1,number,evensum=0,oddsum=0;
      cout<<"Enter The Number:"<<endl;
        cin>>number;
      int temp=number;
        while(i<=number)
        {
            if(i%2==0)
            {
                cout<<i;
                cout<<"\t";
                evensum=evensum+i;
                i++;
            }
            else
            {
                cout<<i;
                cout<<"\t";
                oddsum=oddsum+i;
                i++;
            }
        }

            cout<<"\nSum Of Even Numbers: "<<evensum<<endl;
            cout<<"Sum Of Odd Numbers: "<<oddsum<<endl;


    return 0;
 }
Out Put:
Enter The Number:
6
1       2       3       4       5       6
Sum Of Even Numbers: 12
Sum Of Odd Numbers: 9

No comments:

Post a Comment