Or learn to use a debugger...
Printable View
Or learn to use a debugger...
i traced it many times with a debugger
my logic is like this
for index=1 add to it the next numbers till you get you input number.
1+2+3
then you keep the last number which was inputed
and subtract it from the sum till you get 0
and then keep the smallest
and with a loop print the numbers from the smallest till the biggest
and adding them together till i get the sum-1 before .
how to do this any simpler?
my problem is in the last for
it prints also the next unwanted number
21=10+11+12
i dont khow how to block this 12
I solved it but now i need to post a massage where
there is no way to construct the input number from sequence numbers
it puts the massage "no way" in every case.
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;jndex=jndex+1){//start for
count_up=count_up+jndex;
if (count_up<=num){
printf("+%d",jndex);
count=1; //when we can construct i put 1 into the counter so the last if will not work
}
}//end for
printf("\n");
}//end if equal
}//end for1
if (count!=1){
printf("no way"); //here is the massage
}
//end of option1 code
return 0;
}//end main
You try to use count as a flag to see if a print succeeded -- except of course you're already using the variable count for something else, so it gets overwritten. You need a dedicated variable to be your "count succeeded" flag.
thanks it works