- 3
why my commented (//....) code is not working?
#include<iostream> using namespace std; int main(){ // int sum=0; // cout<<"Enter number"<<endl; // cin>>k; // for(int i=0; 1<i<=9;i++){ // if(i%2==0){ // sum=sum+i; // } // else{ // return 0; // } // //cout<<sum; // } //} int sum=0, k; cout<<"enter number"; cin>>k; for(int i = 0; i <= k; i++) if(i % 2 == 0) sum += i; cout << sum << endl; }
6 Respuestas
+ 2
Why do you repeat every question you made?
https://www.sololearn.com/discuss/2736984/?ref=app
https://www.sololearn.com/discuss/2736979/?ref=app
https://www.sololearn.com/discuss/2736978/?ref=app
https://www.sololearn.com/discuss/2736981/?ref=app
https://www.sololearn.com/discuss/2736980/?ref=app
https://www.sololearn.com/discuss/2736983/?ref=app
https://www.sololearn.com/discuss/2736982/?ref=app
+ 3
♨ Soumik 🎶 Yes maybe but if she can add more details like what she is doing here so we can tell.
+ 2
Because when you consider a line of code as a comment, compiler ignores that line while running.
+ 1
Adeeba
Comments are used for understanding purpose means anyone can understand easily what is going on in the code. Compiler ignore comments to execute.
0
First Problem:
int sum=0;
...
cin>>k;
=> there ist no k. sum ist the only variable you declared.
Second Problem:
for(int i=0; 1<i<=9;i++)
Here is an explanation what happens in this code:
"1 < i <= 9" translates to
(1< i) <= 9, which results in either
0 <= 9 or 1 <= 9 (depending on i)
And this expression is always true. You basically create an infinite loop.
And here is a description of how to achieve what you want:
To express "a < b < c" you need to split it up into "a<b" and "b<c". Combine these with a logical 'and' operator (a < b && b<c).