11111
11111
Last edited by transgalactic2; 12-05-2008 at 12:55 PM.
And also this sentence makes no sense:
I have no idea what you think you mean there.But the debugger shows that it doesnt go to the main for
so it does only the option for 1.
??
But anyway, you need to set count equal to 0 each time, before you start adding things to it.
111111
Last edited by transgalactic2; 12-05-2008 at 12:55 PM.
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.....
1111
Last edited by transgalactic2; 12-05-2008 at 12:55 PM.
thanks i solved it
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("%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
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.
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.
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("%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
Your else statement didn't include "return", did it?
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("%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