int main(int argc, const char * argv[])
{
// declare integer i
int i;
// create loop starting at 99, decrementing 3, down to 0
for (i = 99; i>0; i -=3) {
// allow user to input where to start, but only ask once in the loop
if (i==99) {
// ask where to start counting
printf(“Where should I start counting? “);
// insert readline function to take user input
const char *num = readline(NULL);
// insert atoi function to change input to type integer
i = atoi(num);
}
// display the current number in the countdown
printf(”%d\n”, i);
// if the number is evenly divisible by 5 dispaly Found one!
if (i % 5 == 0) {
printf(“Found one!\n”);
}
}
return 0;
}
[/code]
Seems a bit too complex. the for-loop is ok, but why have code in there that is only executed the first time? This code can be put before the for-loop. An added benefit is that the variable i is not changed in the loop itself, which makes the code clearer.
#include <stdio.h>
#include <stdlib.h>
#import <readline/readline.h>
int main(int argc, const char * argv[])
{
// ask where to start counting
printf("Where should I start counting? ");
// insert readline function to take user input
const char *num = readline(NULL);
// insert atoi function to change input to type integer
// create loop starting at 99, decrementing 3, down to 0
for (int i = atoi(num); i >= 0; i -= 3) {
// display the current number in the countdown
printf("%d\n", i);
// if the number is evenly divisible by 5 dispaly Found one!
if (i % 5 == 0) {
printf("Found one!\n");
}
}
return 0;
}
I don’t think your solution is complex as stated above.
Here is the solution that I liked. As opposed as pretty much everyone, I used i++ as my increment and then added 2 if statements:
#import <readline/readline.h>
#include <stdio.h>
#import <stdlib.h>
int main(int argc, const char * argv[]) {
printf("Where should I start counting?");
const char *number = readline(NULL);
int i = atoi(number);
for (i = i; i >= 0; i--) {
if (i % 3 == 0) {
printf("%d\n",i);
if (i % 5 == 0) {
printf("Found one!\n");
}
}
}
return 0;
}