Write a C++ code segment using the while loop to find the sum of the even integers 2,4,6,8,...,500. Display the resulting sum. Be sure to give code to declare and initialize variables used.
This is what I have so far, but I'm not sure if it's correct or not, nor do I really know how to progress. Feedback/assistance would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
usingnamespace std;
int main()
{
int number1 = 2
while (number1 + 2 <= 500)
{
So it looks like you started with the right idea with starting with the number 2, which is certainly an even number. At this point all you would need to do is generate the next even number which is relatively straight forward, adding 2. So with this in mind you would need to take this value and add it to some total value, which implies another variable of say sum. So you could maybe have something along the lines of
1 2
sum = sum + number1;
number1 = number1 + 2;
Of course, this requires some initialization of an additional variable with the appropriate starting value. I'll let you figure this part out. I don't want to steal all the fun ;)
#include <iostream>
usingnamespace std;
int main()
{
int number1 = 2
int sum = 0
while (number1 + 2 <= 500)
{
number1 = number1 +2
sum = sum + number 1
}
cout<<"Sum = "<<sum<<endl;
return 0;
}