+ 4
Now i got my 1st coding question done.. Tq guys
4 Antworten
+ 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;
}
+ 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;
}
0
cout<<"the sum are:"<<sum<<endl;
should be outside of the loop after finishing summing the numbers.
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