Tuesday, 12 August 2014

C plus plus program to find the sum of odd and even numbers from 1 to Number

Leave a Comment
/*
 C plus plus program to find the sum of odd and even numbers from 1 to Number
 */
#include<iostream> //header files
#include<conio.h>
using namespace std;
void main()//main function
{
    int num, osum = 0, esum = 0; //veriables declaration

    cout<<"Enter the value of num = "; //Prompt
    cin>>num;//geting number
    for (int i = 1; i <= num; i++) // loop will num variable time.
    {
        if (i % 2 == 0)    // if structure for even number
            esum = esum + i;
        else
            osum = osum + i; // else for odd number
    }
    cout<<"Sum of all odd numbers  =  "<<osum<<endl; //output
    cout<<"Sum of all even numbers = "<<esum;//output
getch();
}

If You Enjoyed This, Take 5 Seconds To Share It