Finally did a challenge without looking it up!

Here is my take on the challenge.

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

int main(int argc, const char * argv[])
{
// Asks the user for input
printf(“What number do you want it to start?\n”);
const char *input = readline(NULL);
int num = atoi(input);
printf(“Starting at %d\n\n”, num);

// Works out the LOOP statements as long as x is greater or equal to 0
while (num >= 0) {
    printf("%d\n", num);
    num -= 3;
    if (num % 5 == 0) {
        printf("Found one!\n");
    }
}

return 0;

}[/code]

and it works good.

[b]What number do you want it to start?
2200

Starting at 20

20
17
14
11
8
Found one!
5
2
Program ended with exit code: 0[/b]

Actually, I believe you need to move the decrement statement to the end of the loop as 20 should get a “Found one!”.

Here is my code:

#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {
    
    
    printf("Where should I start counting? ");
    const char *start = readline(NULL);
    
    int i = atoi(start);
    while (i >= 0) {
        printf("%d\n", i);
        if (i % 5 == 0) {
            printf("Found one!\n");
        }
        i -= 3;
    }
    return 0;
}

My solution as below:

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

int main(int argc, const char * argv[]) {
printf(“Were should I start counting?\n\n”);
const char * input = readline(NULL);
int x = atoi(input);
printf("%d\n", x);
do {
int countDown = x - 3;
printf("%d\n", countDown);
if (countDown % 5 ==0){
printf(“Found One!\n”);
}
x-=3;
}while (x > 0);

return 0;

}
[/code]

Good work there TS, but Im not as smart, have to looking for answer here only to find out:

  • I didn’t include the readline.h and stdlib.h in my programme, the book doesn’t seems to show it either :open_mouth:
  • atoi can work with variables
    (previously I used int x = atoi ("%s", input), which is funny)

Tho this is my first time through this book, I still couldn’t really do the challenge without looking it up here. I’m very thankful for this forum btw. Should I be able to do this stuff on my own? is this normal or ?

Everyone comes at this stuff with a different amount of programming experience, and many with none at all. If you’re having difficulty with the challenges, don’t give up. Get through the rest of the book, and then maybe even consider going through it a second time, seeing how much stuff that didn’t make sense before makes sense now that you’ve done it before. Then try the challenges again. If you’re still having trouble after that, try talking to a non-programmer about them - that is, think about how you would solve the problem without code, and then think about how you’d represent the solution in Objective-C.

Also, leverage the heck out of the documentation and Apple’s class references. And us. If you have questions about challenges, many of the folks here are happy to explain how they came up with their solutions.