Question-Implement the following procedure to generate prime
numbers from 1 to 100 into a program. This procedure is
called sieve of Eratosthenes.
step 1 Fill an array num[100]with numbers from 1 to 100
step 2 Starting with the second entry in the array, set all its
multiples to zero.
step 3 Proceed to the next non-zero element and set all its
multiples to zero.
step 4 Repeat step 3 till you have set up the multiples of
all the non-zero elements to zero
step 5 At the conclusion of step 4, all the non-zero entries
left in the array would be prime numbers, so print
out these numbers.
Solution- A good question . Actually I have written a tedious solution for that. Try to go through it . if doesn't work. write to me.
#include<stdio.h>
#define m 100
int main()
{
int arr[m],i,j,k,a;
for(i=0;i<=m-1;i++)
arr[i]=i+1;
for(j=2;j<=m-1;j++)
{
a=2;
while(a<=100)
{
k=arr[j]/a;
if(arr[j]%a==0&&arr[j]!=a)
arr[j]=arr[j]-a*k; /*Setting an element to 0 , you could do it also like arr[j]=0 */
a++;
}
}
printf("Prime numbers between 1 to 10 are\n");
for(i=1;i<=m-1;i++)
if(arr[i]!=0)
printf("%d ",arr[i]);
return 0;
}
numbers from 1 to 100 into a program. This procedure is
called sieve of Eratosthenes.
step 1 Fill an array num[100]with numbers from 1 to 100
step 2 Starting with the second entry in the array, set all its
multiples to zero.
step 3 Proceed to the next non-zero element and set all its
multiples to zero.
step 4 Repeat step 3 till you have set up the multiples of
all the non-zero elements to zero
step 5 At the conclusion of step 4, all the non-zero entries
left in the array would be prime numbers, so print
out these numbers.
Solution- A good question . Actually I have written a tedious solution for that. Try to go through it . if doesn't work. write to me.
#include<stdio.h>
#define m 100
int main()
{
int arr[m],i,j,k,a;
for(i=0;i<=m-1;i++)
arr[i]=i+1;
for(j=2;j<=m-1;j++)
{
a=2;
while(a<=100)
{
k=arr[j]/a;
if(arr[j]%a==0&&arr[j]!=a)
arr[j]=arr[j]-a*k; /*Setting an element to 0 , you could do it also like arr[j]=0 */
a++;
}
}
printf("Prime numbers between 1 to 10 are\n");
for(i=1;i<=m-1;i++)
if(arr[i]!=0)
printf("%d ",arr[i]);
return 0;
}
in the printf tokin you write "Prime Number between 1 to 10 are"
ReplyDeleteinstead of "prime Number between 1 to 100 are"
Right it
otherwise your performance is goood.......