Software & Finance





Visual C++ - Sum of EVEN Numbers in the Given Range





Here is the Visual C++ program for the sum of EVEN Numbers given a range.

 


Source Code


#include <iostream>

 

void main()

{

    int index, begno, endno, sum = 0;

    std::cout << "Program for sum of even numbers in the given range\n";

 

    std::cout << "Enter Beg. No.: ";

    std::cin >> begno;

    std::cout << "Enter End. No.: ";

    std::cin >> endno;

    index = begno;

    if( (begno % 2) == 1) // If it ODD, then make it EVEN

        index = begno + 1;

    for(; index <= endno; index += 2)

    {

        sum = sum + index;

    }

    std::cout << "The sum of even numbers between " << begno << " and " << endno << " is: " << sum << "\n";

}

Output


 

Program for sum of even numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of even numbers between 1 and 100 is: 2550