Tuesday, 7 August 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

No comments:

Post a Comment