-->
Showing posts with label first twenty prime numbers in given range. Show all posts
Showing posts with label first twenty prime numbers in given range. Show all posts

program to display first 20 prime numbers between 0 and 500

-----------------------------------------------------------------------------------------------------
// first twenty prime numbers lying in given range 0 and 500
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,count,first=0,total=0;
for(i=1;i<=500;i++)
{
    if(i<=3)
{
 printf("%d\n",i);

}
      else
      {
     count=0;
       for(j=2;j<=i-1;j++)
 {
   if(i%j==0)
     {
      count=1;

     }
 }
       }

 if(count==0 )

  {
    total++;

if(total<=17 )

{
 printf("%d\n",i);
}
  }
}
getch();
}
--------------------------------------------------------
logics in mind:-
-------------------
->we have to display first twenty prime numbers in given range 0 and 100
->numbers are 1,2,3,,5,7,....
  we can see that first three numbers are 1,2,3 prime so for them we have used loop with 'if'
  If we count then we have three numbers  as a total between 1 and 3.
-> as the number(i) exceeds 3 it transfers the control to next loop where that number is divided from 2 to  i-1. We are not taking 1 because we want to know , is there any number which can divide with remainder '0' or not.
->We want to know here , how many numbers are there which can divide completely with remainder '0'.
->To know that, we have used one variable 'count'. If it is divisible by by some other numbers then 'count' becomes '1'. otherwise it remains '0'.And to display first 17 numbers(3 already gone) we have used another variable as as a counter named 'total'.
->If the variable 'count' has same value '0' and total is less than 17   then we display that value using variable 'i'.