Monday, 5 December 2016

C program to display prime numbers in a range

Prime numbers are numbers which are divisible by 1 and itself. Hence the number of divisors for a prime number is exactly two. This logic is used to print the prime numbers between two limits.

//Program to display prime numbers between two limits
#include<stdio.h>
#include<conio.h>
main()
{
int m, n, c, j;

clrscr();
printf("Enter the lower and upper limits \n");
scanf("%d%d",&m,&n);
printf("\n The prime numbers between the limits %d and %d\n",m,n);
printf("-------------------------------------------------\n");
while (m<=n)
{
    c=0;
    for(j=1;j<=m;j++)
    {
        if   (m%j==0)
            c++;
    }
    
    if   (c==2)
          printf("%d\n",m);

    m++;
}

getch();

}


The output of the program is as follows
OUTPUT
------------
Enter the lower and upper limits
5
50

 The prime numbers between the limits 5 and 50
----------------------------------------------------------
5
7
11
13
17
19
23
29
31
37
41
43
47


 

No comments:

Post a Comment