C Program to Generate Prime Numbers in Given Minimum to Maximum Ranges
A Prime Number is a positive integer greater than 1 which is divisible by 1 and itself. In other words, Prime number is a whole number greater than whose factors are 1 and itself. Examples of first few prime numbers are 2, 3, 5, 7, 11, 13..
In this C program, we are going to generate such Prime numbers from minimum to maximum value which are given by users.
Program
#include<stdio.h>
#include<conio.h>
int main()
{
int minimum, maximum, flag, i, j;
clrscr();
/* Inputs */
printf("Enter minimum number: ");
scanf("%d", &minimum);
printf("Enter maximum number: ");
scanf("%d", &maximum);
/* Generating prime numbers */
for(i=minimum; i<=maximum; i++)
{
flag = 0;
for(j=2; j <= i/2; j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0 && i>=2)
{
printf("%d\t",i);
}
}
getch();
return(0);
}
Output of the above program :
Run 1:
--------------
Enter minimum number: 100 ↲
Enter maximum number: 200 ↲
101 103 107 109 113 127 131 137 139 149
151 157 163 167 173 179 181 191 193 197
199
Run 2:
--------------
Enter minimum number: -100 ↲
Enter maximum number: 10 ↲
2 3 5 7
Note: ↲ indicates enter is pressed.