+ 4

Now i got my 1st coding question done.. Tq guys

https://code.sololearn.com/cqM7mUlD4gPB/?ref=app

3rd Oct 2017, 11:57 PM
muhammad zikri ismail nasaruddin
muhammad zikri ismail nasaruddin - avatar
4 ответов
+ 15
You forgot to initialise sum to 0. For loop syntax is wrong, supposed to contain semicolons instead of commas. #include <iostream> using namespace std; int main() { int n,sum = 0,number; cout<<"input your n"<<endl; cin>>n; cout<<"the even number are:"<<endl; for (number=2; number<=n; number+=2) { cout<<""<<number<<endl; sum=number+sum; cout<<"the sum are:"<<sum<<endl; } return 0; }
3rd Oct 2017, 11:59 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
look like you missed semicolon instead of colon when creating for loop here is the correction #include <iostream> using namespace std; int main() { int n,sum,number; cout<<"input your n"<<endl; cin>>n; cout<<"the even number are:"<<endl; for (number=2;number<=n;number+=2) { cout<<""<<number<<endl; sum=number+sum; cout<<"the sum are:"<<sum<<endl; } return 0; }
4th Oct 2017, 12:02 AM
Nura Programmer
Nura Programmer - avatar
0
cout<<"the sum are:"<<sum<<endl; should be outside of the loop after finishing summing the numbers.
4th Oct 2017, 12:08 AM
John Wells
John Wells - avatar
0
and also have to check if its even or not by using % the remainder operator inside your for loop like if(number % 2 == 0){ //output it } //else don't print it since its not even
4th Oct 2017, 12:11 AM
Nura Programmer
Nura Programmer - avatar