Write a program to generate all prime numbers with in a range.

/*Write a program that generate prime numbers with in a range(1-100), procedure is given::
step 1) Fill array with elements, 1 to 100
step 2) star from first element and set all its multiples to zero
step 3) proceed to next nonzero element and set all its multiples to zero
step 4) repeat step3 till you have set up the multiples of all the non zero elements to zero
step 5) after step4 all the remaining non zero elements are prime numbers inbetween that range
*/

#include<stdio.h>

main()
{
int a[100];
int i,j;
printf("\nEnetr the elements of array, not more then 100:: ");
for(i=0;i<100;i++)
scanf("%d",&a[i]);
for(i=0;i<100;i++)
for(j=i+1;j<100;j++)
{ if(a[i]== 0 || a[i] == 1) break;
else if(a[j]%a[i] == 0) a[j]=0;
}

printf("\nPrime numbers:: ");
for(i=0;i<100;i++)
{ if(a[i]!=0)
printf("%d ",a[i]);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s