Sum of even numbers to 100
Write a program using a while loop that calculates and prints the sum of all even numbers between 1 and 100.
Make sure to declare your sum variable outside the loop.
Answer
#include <iostream>
int main()
{
int sum = 0;
int i = 0;
while (i < 100)
{
sum += i;
i+=2;
}
std::cout << sum;
}
2550