Challenge: Counting down

Here’s my code:

int main(int argc, const char * argv[])
{
for (int i = 100; i > 0; i–) {
if (i % 3 == 0) {
printf("%d. Aaron is Cool\n", i);
if (i % 5 == 0) {
printf("%d. Found One!\n", i);
}
}
}
return 0;
}

and my code from this challenge - I added condition when 0 is calculated.

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

    int i;
    
    printf("Looking for number which is divisible by 5. We are starting at 99.\n");
    
    for (i = 99; i >= 0; i -= 3)
    {
        if (i == 0) {
            printf("Checking i = %d.\nDividing 0 by number has no rest - it is 0 always.\n", i);
            break;
        }
        
        if (i % 5 == 0) {
            printf("Found one! %d.\n", i);
            continue;
        }

       printf("Checking i = %d\n", i);
        
    }
    
    printf("The End.\n");
    
    return 0;
}

it works :smiley:

file: main.c

[code]#include <stdio.h>

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

// Challenge: Counting down (Chapter 8 Loops)
// Program that counts backward from 99 through 0 by 3, printing each number
// If the number is divisible by 5, it should also print the words "Found one!".

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

return 0;

}[/code]
output:
99
96
93
90
Found one!
87
84
81
78
75
Found one!
.
.
.
etc

My answer. Didn’t understand why we need to add break or continue for this. :unamused:

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

This was my first solution and seems to me to be the simplest and most compact.

int main(int argc, const char * argv[]) { int i; for (i = 99; i >= 0; i--) { printf("%d\n", i); if (i % 5 == 0) { printf("Found one!\n"); } } }

[quote=“Ciprian”] if (i % 5 == 0) the sign “%” is my problem. I know that is a modulus… and calculate the remainder, but in this case what is the point of entire " if (i % 5 == 0)" can somebody explain me step by step what happens under the hood?
[/quote]

if (i % 5 == 0 ) is looking for cases where i divided by 5 has a remainder of zero. This would indicate that it’s a multiple of 5.

I came up with the following code for this challenge. However, I decided to just print “Found one!” for each multiple of 5 instead of printing number also.

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
for (int i = 99; i >= 0; i-=3) {
if (i % 5 == 0) {
printf(“Found one!\n”);
} else {
printf("%d\n", i);
}
}

return 0;

}[/code]

It’s a ghost town in here!

For anyone who was bugged by the last “I found one!” this stops at 0.

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

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


return 0;

}[/code]

I took test the code. It is working. For Loop use i++ or i–.

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

    int i;
    
    for (i = 99; i < 100; i--) {
        if (i < 0) {
            break;
        }
        if (i % 3 == 0) {
            printf("%d\n", i);
            if (i % 5 == 0){
                printf("Found out!\n");
                continue;
            }
        }
    }
    return 0;
}

I hope this isn’t the dumbest question ever asked but what does the “%” mean in this if if ( i % 5 == 0) ?

It’s the modulus operator. It really just means “give me the remainder of division”.

Here’s an example: 15%6 is 3, because when you divide 15 by 6, you get 2 with a remainder of 3, the complete answer being 2 and 3/6, or 2.5. But it’s the remainder - the 3 - that the modulus operator is for.

It’s common to use the modulus operator to find out if one number is evenly divisible by another: x%y == 0, then x is evenly divisible by y.

Been away for a while, but recently picked up the book and started again from where I left off. Here’s my answer:

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

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

    const char *startString = readline(NULL);
    
    int startCount = atoi(startString);
    
    while (startCount >= 0) {
        printf("%d\n", startCount);
        if (startCount % 5 == 0) {
            printf("Oh, %d is divisible by 5!\n", startCount);
        }
        startCount -= 3;
    }
    return 0;
}

Gives me the output:

Where should I start counting from? 110000

100
Oh, 100 is divisible by 5!
97
94
91
88
85
Oh, 85 is divisible by 5!
82
79
76
73
70
Oh, 70 is divisible by 5!
67
64
61
58
55
Oh, 55 is divisible by 5!
52
49
46
43
40
Oh, 40 is divisible by 5!
37
34
31
28
25
Oh, 25 is divisible by 5!
22
19
16
13
10
Oh, 10 is divisible by 5!
7
4
1
Program ended with exit code: 0