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************/


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

Related program:

  1. Generate first n prime number C program
  2. Search for prime number C program
  3. Flowchart for search prime number

5 comments:

  1. This is the second time I've been to your site. Thank you for providing more information.
    Also visit my web-site :: Remove Decals

    ReplyDelete
  2. Can u explain the program with example please.

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

    ReplyDelete
  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... :)

    ReplyDelete