## 8.08.2012

### Print prime number 1 to 100

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 numberbetween 1 to 100 C program

Related program:

1. Can u explain the program with example please.

2. the program has an error

1. #include
#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();
}

3. the above program for(div=1;div<n;div++)
then you get correct output

4. Thank U Dinesh Bera..,
Finding prime numbers in this way helped me a lot in debugging my Assembly Code for finding N prime numbers... :)

5. Code Dumped :P give correct code

6. I cannot understand the if else condition you used in prime number program

7. Formulate 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?

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

9. ty for easy understanding

10. Why u hv used p

11. Why u hv used p

12. in the beginning of the program if you write p=1; then output will be show 2 also...
i.e int num,n,div,p=1;

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

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

15. Ah lao mitron shat partishat working code prime numbers da. The code is little bit lengthier but very clear and simple to understand.
---------------------------------------------------------------
#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;
}

16. Prime numbers are divisible by 1 or itself.
So 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

17. i could not understand use of if condition can anyone explain abt of this plz

18. sir can not c language all plz u tell me the start mail or any other source