Challenge Using readline() and atoi()

My solution. It works, but any input is appreciated :slight_smile:

[code]#include <stdio.h>
#include <stdlib.h>
#import <readline/readline.h>

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;
}

[quote] for (i = i; i >= 0; i--) { ... } [/quote]
Although the above statement is syntactically correct, it is yucky!

for-statement := for (init-expr ; test-expr ; iter-expr) statement

init-expr, test-expr, and iter-expr are all optional.

Therefore, the above statement can be simplified as:

for (; i >= 0; i--) {
   ...
}

.

[quote=“ibex10”][quote] for (i = i; i >= 0; i--) { ... } [/quote]
Although the above statement is syntactically correct, it is yucky!

for-statement := for (init-expr ; test-expr ; iter-expr) statement

init-expr, test-expr, and iter-expr are all optional.

Therefore, the above statement can be simplified as:

for (; i >= 0; i--) {
   ...
}

.[/quote]

^ This is correct. I just wrote it that way not to confuse people. But you’re absolutely right.