My solution to this. I found this to be much easer than a couple of the previous challenges.
[code]#include <stdio.h>
int main(int argc, const char * argv[])
{
// set int i to be 99; check if i is bigger than 0, actually bigger than -1 to get the final 0 in output; subtract 3 from i
for (int i = 99; i > -1; i = i-3) {
printf("%d\n", i);
// check if i is divisible by 5
if ( i % 5 == 0) {
printf("Found one!\n");
continue;
}
}
return 0;
@themako1 the “==0” is stating that you want a remainder of only 0 which makes the “i” in the equation a multiple of 5. Then, if it does, you want to output to print, “Found one!”.
I have to admit I like the choice of for loop conditional much better in the solutions you guys posted. The way I did mine (i < 100) means that I used a break statement.
Also I really like the way you guys chose to increment (de-increment?) i as well. I just did i– but that means I needed an extra conditional statement.
It’s crazy to see hakuin using 4 lines of code to achieve what I did in 14! Very enlightening!
Anyway, here’s mine:
#include <stdio.h>
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 One!\n");
}
} else {
continue;
}
}
return 0;
}
On a side note, I See you guys didn’t declare i, prior to using it in your for loops. When I try this (remove the line int i;) I get a warning about using an ‘undeclared identifier’ - any ideas?
I managed to understand almost everything, but this line of code I cant warp my head around it " 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?
[code]#include <stdio.h>
int main(int argc, const char * argv[])
{
int i;
for (i = 99; i > 0; i-=3) {
printf(" %d \n", i);
if (i % 5 == 0 ) {
printf("Found one:%d\n", i);
}
}
return 0;
}
[/code]
The output is o.k.
I’m waiting your answer…
[quote=“Ciprian”]I managed to understand almost everything, but this line of code I cant warp my head around it " 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?
[code]#include <stdio.h>
int main(int argc, const char * argv[])
{
int i;
for (i = 99; i > 0; i-=3) {
printf(" %d \n", i);
if (i % 5 == 0 ) {
printf("Found one:%d\n", i);
}
}
return 0;
}
[/code]
The output is o.k.
I’m waiting your answer…[/quote]
If i divided by 5 gives a remainder equal to 0, then print “Found One”
If the remainder is equal to 0, then it has divided “properly” so 5 must divide successfully into this number.
[quote=“Ciprian”]I managed to understand almost everything, but this line of code I cant warp my head around it " 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?
Here’s what I came up with. It is very similar to most. I just thought I’d add some lines so after finding a number that is divisible by 5 for the first time it would show Found One! followed by the number. Subsequently, it shows Found Another! followed by the number.
#include <stdio.h>
int main(int argc, const char * argv[])
{
int i = 0;
for (i = 99; i > -1; i -= 3) {
printf("%d\n", i);
if (i % 5 == 0) {
int foundNumberDivisibleBy5;
if (foundNumberDivisibleBy5 < 1)
{
printf("Found one! %d\n", i);
foundNumberDivisibleBy5++;
} else {
printf("Found another! %d\n", i);
}
}
}
return 0;
}
Here is the result:
99
96
93
90
Found one! 90
87
84
81
78
75
Found another! 75
…
Found another! 15
12
9
6
3
0
Found another! 0
Program ended with exit code: 0
[quote] // Challenge 1
for (int i = 99; i >= 0; i -= 3) {
(i % 5) ? printf("Found one! %d\n", i) : printf("The number is %d\n", i);
}[/quote]
No! The ternary operator was not meant to be used this way.
The ternary operator requires three expressions as operands, thus using it as a substitute for an if-else statement may not always work. The reason it works in the code above is that printf statement happens to return an int value.
Better to write the above code like this:
for (int i = 99; i >= 0; i -= 3) {
if (i % 5) {
printf ("Found one! %d\n", i)
}
else {
printf ("The number is %d\n", i);
}
}
And use the ternary operator as it was intended to be used:
I do see what you are saying, and after researching ternary (conditional) operators a bit more, I now understand that one big advantage (as your example shows) is that it is an expression, not a statement like an if-else, and as such I can see a number of bonafide uses for it.
I will re-think my use of it, though I still do like my use in this case, though I guess i could have also done:
if (i % 5) { printf("Found one! %d\n", i); } else { printf("The number is %d\n", i); }
Here’s my take on it, I too was obsessed (focused?) with using break and continue. Trying my best to include comments for my own learning so, enjoy my thoughts. Honestly, it annoys me how messy it looks but the comments do help me.
// Declare an integer variable "i"
int i;
// Insert a for loop, assign the variable "i" a value of 99
// and use decrement operator -- to count backwards.
// Used comparison operator <= to include 99 in the sequence
// or could have just set int value to 100 and set it as
// i < 100.
for (i = 99; i <= 99; i--) {
// How do I count backwards by 3?
// Tried out the comparison operator != with 'continue' since
// we want to include multiples of 3, not exclude. Seems to work.
if (i % 3 != 0) {
continue;
}
// Use modulus to find multiples of 5
printf("%d\n", i);
if (i % 5 == 0) {
printf("Found one!\n");
}
// Insert 'break' to stop program and keep it from infinitely
// counting backwards by 3.
if (i == 0) {
break;
}
}
return 0;
}
[quote]
...
// Declare an integer variable "i"
int i;
...
for (i = 99; i <= 99; i--) {
...
// Insert 'break' to stop program and keep it from infinitely
// counting backwards by 3.
if (i == 0) {
break;
}
}
[/quote]
Keep it simple:
// Counting down from 99
for (int i = 99; i >= 0; i--) {
...
}
[quote=“Ciprian”]I managed to understand almost everything, but this line of code I cant warp my head around it " 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?
[code]#include <stdio.h>
int main(int argc, const char * argv[])
{
int i;
for (i = 99; i > 0; i-=3) {
printf(" %d \n", i);
if (i % 5 == 0 ) {
printf("Found one:%d\n", i);
}
}
return 0;
}
[/code]
The output is o.k.
I’m waiting your answer…[/quote]
(i realize this is three months old but someone else could have the same question)
So the challenge requires our program to print “Found one” for any of the numbers that are divisible by 5.
If i were to print the output of i % 5, it would take whatever number i currently is and divide it by five and output the remainder. So the numbers divisible by 5 are ones that I can divide by 5 and the remainder would be 0. For example: 25/5 is 5, so there is no remainder. Same as 50/5 is 10 and there is no remainder. For these two cases 25 % 5 would be 0, and 50/5 would be 0. The expression would evaluate true and print out Found one.