Prime Number between Intervals

Prime number interval

Prime numbers

The numbers which are divisible only by 1 and by the number itself are known to be prime numbers. Prime numbers are always greater than 1. The only even prime number is 2 while the rest of the prime numbers are odd.We will learn how to find prime number between intervals.

Print prime number between intervals:

Step 1 : Take the values of lower limit and upper limit of the interval as input.
Step 2 : Initialize a variable flag with value 0.
Step 3 : Iterate a for loop from lower limit to the upper limit.
Step 4 : Take the value as num.
Step 5 : Now iterate a for loop from 0 to num/2.
Step 6 : If the num is divisible by loop iterator,then increment flag.
Step 7 : If flag = 0 , then prime
              else , not prime
Step 8 : Back to Step 4.
Step 9 : End

Example 1:

Run

#include<stdio.h>

int main()
{
	int a, b, i, j, flag;
	printf("Enter lower bound of the interval: ");
	scanf("%d", &a); 
	printf("\nEnter upper bound of the interval: ");
	scanf("%d", &b); 
	printf("\nPrime numbers between %d and %d are: ", a, b);
	for (i = a; i <= b; i++) {
		if (i == 1 || i == 0)
			continue;
		flag = 1;
                for (j = 2; j <= i / 2; ++j) {
			if (i % j == 0) {
				flag = 0;
				break;
			}
		}
		if (flag == 1)
			printf("%d ", i);
	}
        return 0;
}

Output:

Enter lower bound of the interval: 1
Enter upper bound of the interval: 15
Prime numbers between 1 and 10 are: 2 3 5 7 11 13

Example 2:

Run
#include<stdio.h>

int main() {
   int low, high, i, flag;
   printf("Enter two numbers(intervals): ");
   scanf("%d %d", &low, &high);
   printf("Prime numbers between %d and %d are: ", low, high);
   while (low < high) {
      flag = 0;
      if (low <= 1) {
         ++low;
         continue;
      }
      for (i = 2; i <= low / 2; ++i) {

         if (low % i == 0) {
            flag = 1;
            break;
         }
      }
      if (flag == 0)
         printf("%d ", low);
      ++low;
   }
   return 0;
}

Output:

Enter two numbers(intervals): 20 
50
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription