0
Can anyone explain why my c program code doesnt show any output
Print prime numbers from 1 to 100 https://code.sololearn.com/cWe3KevDLGIz/?ref=app
7 odpowiedzi
+ 4
Just put c = 0; before second for loop.
+ 1
Check this out
https://code.sololearn.com/c45OoiK5xKHr/?ref=app
You have placed output on the condition that c == 2 . but this never happens in your code.
P.S: I'm curious what your code is for. Can you explain about it some more?
+ 1
See the output of this code, hope it will help you to understand that 'c' will never equal '2' at the end of nested 'for'.
https://code.sololearn.com/cdECrnbxslR9/?ref=app
+ 1
Mihai Apostol it worked when I put c=0 at the beginning of the second loop but what's the difference putting it there and in local declaration
+ 1
Mihai Apostol Thanks!!
0
Ketan Padal
Because variable c works like a counter and it must be reseted back to 0 for each iteration of the first loop ie for every number you check if is prime ie has only two divisors 1 and itself, hence c == 2.
If you don't reset this will accumulate and get bigger and your condition c == 2 will never be met.
Eg.
a = 1 c = 0 b = 1 1%1 = 0 c = 1
no print
a = 2 c = 0 b = 1 2%1 = 0 c = 1
b = 2 2%2 = 0 c = 2
print 2
a = 3 c = 0 b = 1 3%1 = 0 c = 1
b = 2 3%2 = 1 c = 1
b = 3 3%3 = 0 c = 2
print 3
...
...
...
0
Ketan Padal You're welcome.