# Thread: code not working as it should..

1. ## code not working as it should..

11111

2. Hmmm I didn't want to be a jerk but you twisted my arm....

Originally Posted by transgalactic2
Code:
```      void main(                                                                void)
{
int i;
flush(stdin);
return i;
}```

3. And also this sentence makes no sense:
But the debugger shows that it doesnt go to the main for
so it does only the option for 1.
??
I have no idea what you think you mean there.

But anyway, you need to set count equal to 0 each time, before you start adding things to it.

4. 111111

5. Yes, but if you miss you don't bring count back down ever. So in your 21 example, you start with 2 and you add up to 6; that makes the total 22. So that loop stops, the if is false, and we start over with 3. We add 3, and now we're at 25. The if is false, and we start over at 4. We add 4, and now we're at 29. The if is false.....

6. 1111

7. thanks i solved it

8. Don't edit out your problems! It's good reference for people searching for solutions to problems!

9. when i enter 7

it gives me 7=3+4+5
???
Code:
```#include <stdio.h>

int main(){

//start of option1 code
int index,tndex,kndex,jndex;
int num,count,count_up;
count=0;
printf("Enter a natural number\n");
scanf("&#37;d",&num);

for(index=1;index<num;index++){//start for1
count=0;
for(tndex=index;count<num;tndex++){//start for2

count=count+tndex;

}//end for2
count_up=count;

tndex--;
if (count==num){//start if equal
for(kndex=tndex;count>0;kndex--){//start inner for

count=count-kndex;

}//end inner for
kndex++;
printf("%d=%d",num,kndex);
count_up=0;
kndex++;
for(jndex=kndex;count_up<num-1;jndex=jndex+1){//start for

count_up=count_up+jndex;
printf("+%d",jndex);

}//end for
printf("\n");
}//end if equal
else{
printf("X can't be written as sum of sequential integers");
}
}//end for1
//end of option1 code
}//end main```

10. Maybe your program is actually generating results that we have all been overlooking in math these past millenia. Fix your indendation please, and maybe I will read your code.

11. Maybe you should set count_up equal to kndex, instead of 0, since at that point in the program you've already dealt with kndex.

12. The problem is with the ELSE (when it differs).
When i delete the content of else it gives me all the results of 21.
But when i put a massage in it i get only the first resolt of 21

Code:
```#include <stdio.h>

int main(){

//start of option1 code
int index,tndex,kndex,jndex;
int num,count,count_up;
count=0;
printf("Enter a natural number\n");
scanf("&#37;d",&num);

for(index=1;index<num;index++){//start for1
count=0;
for(tndex=index;count<num;tndex++){//start for2

count=count+tndex;

}//end for2

count_up=count;

tndex--;
if (count==num){//start if equal

for(kndex=tndex;count>0;kndex--){//start inner for

count=count-kndex;

}//end inner for
kndex++;
printf("%d=%d",num,kndex);
count_up=0;
kndex++;

for(jndex=kndex;count_up<num-1;jndex=jndex+1){//start for

count_up=count_up+jndex;
printf("+%d",jndex);

}//end for
printf("\n");

}//end if equal
else{

}
}//end for1
//end of option1 code
}//end main```

13. Your else statement didn't include "return", did it?

14. what is the best way to do this thing ??
i am going the wrong way with this code
if you have some algorithm i can write a new code
i dont now where is my problem

i added this return 0 line in the end
but for 21 i get
10+11+12
Code:
```#include <stdio.h>

int main(){

//start of option1 code
int index,tndex,kndex,jndex;
int num,count,count_up;
count=0;
printf("Enter a natural number\n");
scanf("&#37;d",&num);

for(index=1;index<num;index++){//start for1
count=0;
for(tndex=index;count<num;tndex++){//start for2

count=count+tndex;

}//end for2
count_up=count;

tndex--;
if (count==num){//start if equal
for(kndex=tndex;count>0;kndex--){//start inner for

count=count-kndex;

}//end inner for
kndex++;
printf("%d=%d",num,kndex);
count_up=0;
kndex++;
for(jndex=kndex;count_up<num-1;jndex=jndex+1){//start for

count_up=count_up+jndex;
printf("+%d",jndex);

}//end for
printf("\n");
}//end if equal
else{

}
}//end for1
//end of option1 code
return 0;
}//end main```

15. Originally Posted by transgalactic2
what is the best way to do this thing ??
i dont now where is my problem
You could read what people are telling you, that might be a good start. Or you could (heaven forfend) actually trace your code with a piece of paper and watch what happens, that would work too (perhaps even better in the long run).