Challenge


#1

Ok, so I can get the loop to run and countdown from 99 to 0.
But I can’t seem to get it to add in the Found One section for the #'s divisible by 5.

I have been at this function for over an hour and I think I’m choosing the wrong type of loop.

Any suggestions?

Thanks,
jamie


#2

I think you might just need to throw an “if” into what you’re doing. This is what I came up with for it.

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

#3

Hi, I’ve come up with a different code. Suggestions are welcome :slight_smile:

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

#4

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

This is what I came up with to get to zero.


#5

mine is pretty similar to everyone else’s:

[code] int challenge;

for (challenge = 99; challenge >= 0; challenge = challenge - 3) {
    printf("%d\n", challenge);
    if (challenge % 5 == 0) {
        printf("found one!\n");
    }
}[/code]

#6

kevpercha,
You can change your increment/decrement variable multiple times within a function, but you really don’t want to (it makes it more difficult to follow the logic).

My code is pretty similar to everyone else’s.

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


#7

Thanks PHBeagle for pointing out the increment/decrement thing.

Kevin


#8

Here is another version, using continue. Any thoughts or recommendations are appreciated.

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


#9

Regarding my previous post regarding using continue, I just realized it is unnecessary. Removing continue had no effect on the final output.


#10

Glad you guys posted your code, i can see how other operators can come in use for the loop conditional statement.
Here was my attempt, it’s kinda the crazy way of doing it on hindsight!!!

int main (int argc, const char * argv[]) {
    
	for (int i = 99; i > -1 ; i--) {

		if (i % 3 == 0) {   
			printf("%d \n",i );
		}
		
		if ((i % 3 == 0) && (i % 5 == 0)){ 
			printf("Found One! \n");
		} 
	}
	
    return 0;
}

#11

Here’s mine. Pretty much the same as some of the others.

[code]#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]

I do like how this challenge reached back a few chapters!


#12

Thanks Guys!!

I had gotten close with an if else, but I wasn’t able to get it to print 0, because of the order it was performing the steps in

This is what I had:

{

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

return 0;

}

which resulted in almost, but not quite working here is the tale end of what it gives:
Found One!
12
9
6
3
Found One!

Thanks for the clarification…it was a big help :slight_smile:


#13

I’ve been trying to do the past challenges without having to come here but i cant solve it without,

heres my code after i saw a few examples here its basically the same as the first one

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

}
return 0;

}
[/code]

but all it does is state 99 once and thats it, when my code was lil bit different it just looped 99 continuously, rather than just moving on, want to know whats wrong with my code


#14

The tiniest detail
" i=-3;" should be “i-=3;”

“i=-3” sets the value of “i” to -3, which ends the loop instead of performing the operation “i - 3”


#15

Mine is also very similar: int i; for (i = 99; i > -1; i = i -3) { // could have been "i >= 0" printf("i is %d\n", i); if (i % 5 == 0) { printf("Found one!\n"); } }

I am interested in the shorthand for i = i - 3 as i -= 3 – did I just not notice this in the book, or are some of you bringing prior knowledge to the challenge?


#16

It’s at the very end of page 45 and into page 46


#17

The Challenge sample output includes “Found one!” for zero. But is zero really divisible by 5? :smiling_imp:


#18

yes. but 5 is not divisible by zero. :geek:


#19

yes. but 5 is not divisible by zero. :geek:[/quote]

As I was lying in bed last night, trying to settle the thoughts of the day, for some reason I realized that I swapped the order in that snarky statement. I’m glad I didn’t jump out of bed to correct myself, because thankfully you pointed out my mistake, ha! It’s weird how we realize things at odd times; I didn’t think I was still thinking about the book, but I guess I was.


#20

Here is my solution, tried to use as much as I could from what I learned so far - feedback is highly aapreciated

#include <stdio.h>

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

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