logic of the program.
hello guys, can u just explain me the logic that is inside the while loop in the code below. unable to understand the logic of variable c .
printf("Enter Number Of Terms
printf("Prime Numbers Are Follwing
I've got a problem understanding the logics behind the indentation in this piece of code.
c counts times i % j = 0, which will be at least two for every numbers above 1 (j = 1 & j = i). So if c is less or more than two, (divisible with more or less numbers than 1 & i) i is not a prime.
but that program is working, i tried it.. i dont understand why the condition of c=2 is used to print the primes.
because a prime is only divisible by itself and 1. hence if is C is 2...then it is a prime number...c is merely a counter
as fader_berg mentioned...
any prime number above greater thn 1 is only divisibly by 1 or itself.
example, say you enter 5 terms.
so say you want first 5 prime numbers =n
you initialise i=1. Is 1<5? yes, so enter the while loop
enter the for loop since j=1 and j<=i i.e.1<=1
1%1=0 so increment c from 0 to 1.
you CAN'T enter the for loop again since j=2 and 2<=1. So fall through the loops and go to i++
enter the for while loop since 2<=5
enter the if loop and check if 1%1=0. It does so increment c.
Run through the for loop again this since j=2 and 2<=2
enter the if loop and see if 2%2=0. It does so increment c to 2. c=2
Leave the for loop amd go to the second if statement. It says if c==2 print i.
So it will print 2
now follow the same procedure for 3, 4 etc...
thanks a lot.. now i got the idea behind the logic. Am just a beginner in C. thanks again and keep helping.
Originally Posted by bos1234