I´m trying to perform a sum from 50 to 100 using while.
With the following code I´m getting the result 3825, but it is supposed to be 3750.
Where is the mistake?
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
int main()
{
int soma = 0, variavel = 50;
while (variavel <=100)
{soma += variavel;
++variavel;}
std::cout << "The sum of 50 to 100 inclusive is " << soma <<std::endl;
return 0;
}
Oh yes, the 3750 value I got from faulty logic. It is supposed to be 50 (first number of the series) plus 100 (last number) times the number this sum repeats, all divided by two. The 150 value is repeated 51times and not 50 like I first thought.