All C&DS Needs

Tell's You Everything

Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user


/* Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. */
#include<stdio.h>
int main()
{
int n,i,j;
printf("enter number for prime : ");
scanf("%d",&n);
if(n==0)
{
printf("invalid number entered");
return 0;
}
printf("1 ");
for(i=2;i<=n;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
{
if(i==j)
{
printf("%d ",j);
}
break;
}
}
}
return 0;
}

About these ads

4 responses to “Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user

  1. rahul November 17, 2011 at 20:08

    this program cant give prime nos after 2… it gives only 1 and 2 ……. so plz check it again and tell me correct one……..

  2. steven January 31, 2012 at 10:28

    1. write a program to generate the first 10 all integer
    2. write a program to compute s=1/2+2/4+3/6…..21/22

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

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: