**Q.**Write a C program to accept a specific last number and print all the less then or equal prime number.

*or*

**Q.**Write a C program to print the prime number between 1 to n number.

for example: 1 to 100

**Ans.**

__Note:-__

1. 1 is not the prime number.

2. All even number is not the prime number except the number 2.

/*C program to print 1 to 100 prime numbers*/

**#include**<

**stdio.h**>

**#include**<

**conio.h**>

int main()

{

int num,n,div,p;

printf("Enter any number: ");

scanf("%d",

**&**num);

for(n=2; n<=num; n++)

{

for(div=2; div<n; div++)

{

if(n%div==0)

{

p=0;

**break**;

}

p=1;

}

if(p)

printf("\t%d",n);

}

getch();

**return 0**;

}

**/************Output************/**

Screen shot for print the prime number between 1 to 100 C program |

Related program:

Can u explain the program with example please.

ReplyDeletethe program has an error

ReplyDelete#include

Delete#include

void main()

{

int i,j,n,p;

clrscr();

printf("Enter the value:");

scanf("%d",&n);

for(i=2;i<=n;i++)

{

for(j=2;j<i;j++)

{

if(i%j==0)

{

p=0;

break;

}

p=1;

}

if(p)

{

printf("\t%d",i);

}

}

getch();

}

the above program for(div=1;div<n;div++)

ReplyDeletethen you get correct output

Thank U Dinesh Bera..,

ReplyDeleteFinding prime numbers in this way helped me a lot in debugging my Assembly Code for finding N prime numbers... :)

Code Dumped :P give correct code

ReplyDeleteThank you

ReplyDeleteI cannot understand the if else condition you used in prime number program

ReplyDeleteFormulate a model for picking out the prime number from this range and describe the input, processing and output intend in an algorithm to display all the prime numbers between 1 and 100?

ReplyDeleteI need urgent answers please.

What about 2 .. it will not print 2 ..??

ReplyDeletety for easy understanding

ReplyDeleteWhy u hv used p

ReplyDeleteWhy u hv used p

ReplyDeletein the beginning of the program if you write p=1; then output will be show 2 also...

ReplyDeletei.e int num,n,div,p=1;

int num,n,div,p=1; this is the 100% right.....then output will be show 2 also

ReplyDeleteu can think better trick than this ... little bit confusing but will give us output...

ReplyDeleteAh lao mitron shat partishat working code prime numbers da. The code is little bit lengthier but very clear and simple to understand.

ReplyDelete---------------------------------------------------------------

#include

int main()

{

int a[500],n,k=0;

cout << "Program to generate prime numbers" << endl;

cout << "Enter upto which number you wish to generate prime numbers: ";

cin>>n;

if(n==1||n<1)

cout<<"Invalid input";

else if (n==2)

cout<<"List of prime numbers: 2";

else if (n==3)

cout<<"List of prime numbers: 2 3";

else if (n>3)

{

for(int i=4;i<=n;i++)

{

int j=2;

while(j<=i/2)

{

if(i%j==0)

{

break;

}

else if (j==i/2&&i%j!=0)

{

a[k++]=i;

}

j++;

}

}

cout<<"List of prime numbers: 2 3 ";

for(int l=0;l<k;l++)

{

cout<<a[l]<<" ";

}

}

return 0;

}

thanks

ReplyDeletePrime numbers are divisible by 1 or itself.

ReplyDeleteSo we check that it is divisible by any other number .

Loop starts with 2 and should go to num/2 .... Because any number is not divisible by more than its half.

Ex. 10 is divisible by 5 or any less. But is not divisible by any number more than 5. So loop should be modified like this

div=2; div<=(num/2); div++

For any other mail me kamalkamal002@gmail.com