+ 1
I wrote the following code based in c++ and the compiler giving error, please check and correct my programs error, thanks
#include <iostream> using namespace std; int main() { int sum, number, UpperNumber; sum = 0; number = 1; cin >> UpperNumber ; while (number <= UpperNumber) { If (number % 2 == 0) { sum = sum + number; number = number + 1; }} cout << " The sum of all even numbers between 1 and " <<UpperNumber <<"is " <<sum; return 0; }
2 odpowiedzi
+ 10
'If' should be 'if'. There are also some logical errors in there. Please review and compare this version:
#include <iostream>
using namespace std;
int main()
{
int sum, number, UpperNumber;
sum = 0;
number = 1;
cin >> UpperNumber ;
while (number <= UpperNumber)
{
if (number % 2 == 0)
{
sum = sum + number;
}
number = number + 1;
}
cout << " The sum of all even numbers between 1 and " <<UpperNumber <<"is " <<sum;
return 0;
}
+ 1
thanks Hatsy, it's worked