# logic of the program.

• 02-07-2011
vijayshankar
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 .
Code:

```#include <stdio.h> main() {                 int n,i=1,j,c;                 clrscr();                 printf("Enter Number Of Terms ");                 printf("Prime Numbers Are Follwing ");                 scanf("%d",&n);                 while(i<=n)                 {                     c=0;                     for(j=1;j<=i;j++)                     {               if(i%j==0)                                   c++;                     }           if(c==2)           printf("%d        ",i)           i++;         }         getch(); }```
• 02-07-2011
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.
• 02-08-2011
vijayshankar
but that program is working, i tried it.. i dont understand why the condition of c=2 is used to print the primes.
• 02-08-2011
Eman
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
• 02-08-2011
bos1234

any prime number above greater thn 1 is only divisibly by 1 or itself.

example, say you enter 5 terms.
5%1=0 --->c=1
5%2=1
5%3=2
5%4=1
5%5=0---->c=2

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++

i=2 now

enter the for while loop since 2<=5
enter the if loop and check if 1%1=0. It does so increment c.
c=1

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...
• 02-08-2011
vijayshankar
Quote:

Originally Posted by bos1234

any prime number above greater thn 1 is only divisibly by 1 or itself.

example, say you enter 5 terms.
5%1=0 --->c=1
5%2=1
5%3=2
5%4=1
5%5=0---->c=2

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++

i=2 now

enter the for while loop since 2<=5
enter the if loop and check if 1%1=0. It does so increment c.
c=1

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.