|
#include
<iostream.h>
main()
{
//declaration of variables
int sum, number, upperLimit;
//Initialization of the variables
sum = 0;
number = 1;
// Prompt the user to enter upper limit
of integers
cout << “Please enter the upper
limit for which you want the sum ” ;
cin
>> upperLimit;
// using the while loop to find out the
sum of first even integers starting from 1
while(number <= upperLimit)
{
// Adding the even integer to the contents
of sum
if ( ( number % 2 ) == 0 )
{
sum = sum + number;
}
// Generate the next integer by
adding 1 to the integer
number = number + 1;
}
cout << "The sum of even integers
from 1 to “ << upperLimit << “ is " <<
sum;
}
|