C++ Program to print the sum of n even numbers within range using while
Program
#include <iostream>
using namespace std;
int main()
{
int start_range, end_range;
int sum = 0;
cout << "Enter the starting range: ";
cin >> start_range;
cout << "Enter the ending range: ";
cin >> end_range;
cout << "Sum of all even numbers in given range is :";
int i = start_range;
while (i <= end_range)
{
if (i % 2 == 0)
{
sum = sum + i;
}
i++;
}
cout << sum;
return 0;
}
Output
$ g++ sum-of-n-even-numbers-within-range-using-while.cpp
$ ./a.out
Enter the starting range: 14
Enter the ending range: 27
Sum of all even numbers in given range is :140