Find prime numbers in range
In this article, we will understand C program to find prime numbers within a range or between two numbers.
These numbers will be taken as user input.
Before looking at the C program, first let us know what is a prime number.
A prime number is a number greater than 1, which is divisible by 1 and itself. That is, the remainder of division of a prime number is 0 only when it is divided by 1 or itself.
In other words, a prime number has 2 factors: 1 and the number.
C Program
Below is the C program to find prime numbers in a range followed by its explanation.
#include<stdio.h> void main() { // declare variables int start, end, i, flag; printf("Enter two numbers(interval):\n"); // read numbers scanf("%d %d", &start, &end); // check if start number is less than 0 if(start <= 0) { printf("Start number should be greater than 0"); return; } printf("Prime numbers between %d and %d are:\n", start, end); // loop from start to end while (start < end) { flag = 0; // check if current number is prime for(i = 2; i <= start/2; ++i) { // number is divisible by other number if(start % i == 0) { flag = 1; break; } } // number is not divisible by any number if (flag == 0) printf("%d ", start); ++start; } }
Below is the output of program
Enter two numbers(interval):
15 95
Prime numbers between 15 and 95 are:
17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
1. The program first checks if the start number is 0. If it is, then it prints an error message terminates.
2. Then it loops from start number till end number.
3. In every iteration, it checks if the current number is prime or not.
A. To do so, it loops from 2 till half of the number and checks if the number is divisible by the loop variable.
At the start of each loop, we set a
flag
variable to be 0.B. If it is divisible, means that the number is not prime.
The
flag
variable is set to 1 and the loop in step 3A is terminated.C. After the loop, we check the value of
flag
variable to be 0. If it is 0, means that the condition in step 3B was not true and the number is prime. The reason that the loop to check each number to be prime is from 2 till half the number(Step 2) is that if the number is divisible by any of the values till its half, it will be divisible by the values that are greater than its half.
Check prime number program 2
Suppose the start and end numbers are entered in reverse order, that is, start is smaller and end is larger, then we can handle this in C program itself.
#include<stdio.h> void main() { // declare variables int start, end, i, flag; printf("Enter two numbers(interval):\n"); // read numbers scanf("%d %d", &start, &end); // check if numbers are reverse if(end < start) { // swap numbers int temp = start; start = end; end = tem; } printf("Prime numbers between %d and %d are:\n", start, end); // loop from start to end while (start < end) { flag = 0; // check if current number is prime for(i = 2; i <= start/2; ++i) { // number is divisible by other number if(start % i == 0) { flag = 1; break; } } // number is not divisible by any number if (flag == 0) printf("%d ", start); ++start; } }