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

Description:

 Prime number is a number which is exactly divisible by one and itself only   Ex: 2, 3, 5, 7,………

Algorithm:

Step 1: start
Step 2: read n
Step 3: initialize i = 1, c = 0
Step 4:if i <= n goto step 5
            If not goto step 10
Step 5: initialize j = 1
Step 6: if j <= 1 do as the follow. If no goto step 7
            i)if i%j == 0 increment c
            ii) increment j
            iii) goto Step 6
Step 7: if c == 2 print i
Step 8: increment i
Step 9: goto step 4
Step 10: stop 

Flowchart:

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
   int n, i, j, count;
   clrscr();
   printf("Prime no.series\n");
   printf("Enter any number\n");
   scanf("%d", &n);
   printf("The prime numbers between 1 to %d\n",n);
   for(i = 1; i <= n; i++)
   {
      count = 0;
      for(j = 1; j <=i; j++)
      if(i % j == 0)
      {
     count++;
      }
      if(count == 2)
      {
     printf("%d\t", i);
      }
   }
   getch();
}

Input & Output:

Prime no. series 
Enter any number 
10
The prime numbers between 1 to 10
2  3  5  7

Viva Questions:

Q: What is prime number ?

Ans: Prime number is a number which is exactly divisible by one and itself only.

Q: What is an algorithm?

Ans: A step by step procedure is called algorithm.

Q: What is flow chart?

Ans: A pictorial representation an algorithm is called a flow chart.

Q: What is program?

Ans: A collection of statements is called.