Friday, June 18, 2021
Home coding Write a program to find prime number between given range.

Write a program to find prime number between given range.

Given a range of number,we need to find prime number between these range.print their value on screen.

So what is prime number of number?

prime number is a number which has only two factor one is 1 and second is number itself.

Example:

input: 3 30
output:3 5 7 11 13 17 19 23 29 

Logic:

we have to maintain low and high variable to find the prime number between ranges. 

Code-

#include<bits/stdc++.h>
using namespace std;
int main() {
 
   int l,r;
  cout<<"enter both ranges"<<endl;
  cin>>l>>r;
int flag=0;
   while (l<r) 
   {
      flag = 0;
      if (l <= 1)
      {
         l++;
         continue;
      }

      for (int i = 2; i <= l / 2; i++)
      {

         if (l % i == 0) 
         {
            flag = 1;
            break;
         }
      }

      if (flag == 0)
        {
            cout<<l<<" ";
        }


      l++;
   }

   return 0;
}

Output:

3 30
3 5 7 11 13 17 19 23 29 

Time complexity:0(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments