+ 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; }

24th Jan 2018, 8:11 AM
Sameer Memon
Sameer Memon - avatar
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; }
24th Jan 2018, 8:25 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
thanks Hatsy, it's worked
24th Jan 2018, 8:34 AM
Sameer Memon
Sameer Memon - avatar