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

17th Feb 2020, 5:12 PM
Ketan Padal
Ketan Padal - avatar
7 odpowiedzi
+ 4
Just put c = 0; before second for loop.
17th Feb 2020, 5:14 PM
Mihai Apostol
Mihai Apostol - avatar
+ 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?
17th Feb 2020, 5:16 PM
Salman Nazeer
Salman Nazeer - avatar
+ 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
17th Feb 2020, 9:30 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 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
18th Feb 2020, 2:34 AM
Ketan Padal
Ketan Padal - avatar
+ 1
Mihai Apostol Thanks!!
18th Feb 2020, 3:23 PM
Ketan Padal
Ketan Padal - avatar
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 ... ... ...
18th Feb 2020, 6:24 AM
Mihai Apostol
Mihai Apostol - avatar
0
Ketan Padal You're welcome.
18th Feb 2020, 4:15 PM
Mihai Apostol
Mihai Apostol - avatar