I'm a beginner to C as a precursor to learning Objective-C although I've coded in VB .NET and (a long time ago) Javascript and ActionScript.
My exercise is to count backwards from 99 to 0 outputting a message if the number is divisible by 5. I'm able to do this, but can only decrease the counter by one each time. How can I get it to count down in 3s, for example? I tried adding
i = i - 3after if statement, but it seemed to take off 4 - I'm guess i-- was being included to??
My code so far is:
int main (int argc, const char * argv[])
{
//counter
int i;
//count down from 99 to 0
for (i = 99; i >= 0; i--) {
//output value of counter
printf("%d \n", i);
//check if value of counter is divisble by 5
if (i % 5 == 0) {
//output if number divisible by 5
printf("Found one! \n");
//break this iteration
continue;
}
//decrease counter by 3 each time
i = i - 3;
}
return 0;
}
Thanks for the help!

New Topic/Question
Reply




MultiQuote




|