Challenge (CountDown) - Part 1 &2 - Solution

Code:

[code]// Chapter 8 - CountDown

#include <stdio.h>

int main(int argc, const char * argv[]) {

for (int i=99; i>=0; i-=3) {
    printf("%d\n",i);
    
    if (i%5==0) {
        printf("Found one!\n");
    }
}

return 0;

}
[/code]

Console Output:

[quote]99
96
93
90
Found one!
87
84
81
78
75
Found one!
72
69
66
63
60
Found one!
57
54
51
48
45
Found one!
42
39
36
33
30
Found one!
27
24
21
18
15
Found one!
12
9
6
3
0
Found one!
Program ended with exit code: 0[/quote]

Code:

[code]// Chapter 8 - CountDown Pt.2

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

int main(int argc, const char * argv[]) {

const char *startCounting = readline("Where should I start counting?");
int userValue = atoi(startCounting);


for (int i= userValue; i>=0; i-=3) {
    printf("%d\n",i);
    
    if (i%5==0) {
        printf("Found one!\n");
    }
}
return 0;

}
[/code]

Console Output:

[quote]Where should I start counting?4422

42
39
36
33
30
Found one!
27
24
21
18
15
Found one!
12
9
6
3
0
Found one!
Program ended with exit code: 0[/quote]

#include <stdio.h>

int main(int argc, const char * argv[])
{
for (int i = 99; i >=0; i-=3) {
printf("%d.\n", i);

    if (i % 5 == 0) {
        printf("Found one!");
    }
}
return 0;

}

My solutions to parts 1 & 2 differ by one line…may not be the best though.

//  main.c
//  CountDown

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

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

Can I know if i could do the user input challenge like this(challenge no.2)?

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

int main(int argc, const char * argv[])
{
printf(“Where should I start counting?”);

int i;
for (i = 42; i >= 0; i -= 3) {
    printf("%d\n", i);
    
    if (i % 5 == 0) {
        printf("Found one!");
    }
}
return 0;

}

THE OUTPUT IS AS FOLLOWS :-

Where should I start counting?42
39
36
33
30
Found one!27
24
21
18
15
Found one!12
9
6
3
0
Found one!Program ended with exit code: 0

Thanks in advance! :slight_smile:

[quote]Can I know if i could do the user input challenge like this(challenge no.2)?

[code]

#import <readline/readline.h>

int main (int argc, const char * argv[])
{
printf (“Where should I start counting?”);

int i;
for (i = 42; i >= 0; i -= 3) {
printf ("%d\n", i);

if (i % 5 == 0) {
printf (“Found one!”);
}
}
return 0;
}[/code]
[/quote]
The the statement which gets the input from the user is missing.

...
int main (int argc, const char * argv[])
{
   // Prompt for input
   printf ("Where should I start counting?");
   const char * input = ...
   const int startFrom = stringToInt (input);
   ...

Also, you will make it easier for humans if you pretty-format your code and post it between the code tags like this:

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

int main (int argc, const char * argv[])
{
   // Prompt for input
   printf ("Where should I start counting?");
   
   // Get the input
   ...
   const int startFrom = ...

   int i;
   for (i = startFrom; i >= 0; i -= 3) {
         printf ("%d\n", i);

         if (i % 5 == 0) {
              printf ("Found one!");
         }
   }
   return 0;
}