C++ Program For Prime Number Between 1 to N

C++ Program For Prime Number Between 1 to N | A natural number which has only two factors ( 1 and itself ) is called a prime number. For example, 5 is a prime number because it has only two factors 1 and 5. Similarly, 9 is not a prime number because it has more than 2 factors that are 1,3, and 9.

To develop a C++ program to check prime number; first, you should know how to find out all factors of a number. If any number has more than 2 factors then only, it is a prime number. All negative numbers, 0 and 1 are not the prime numbers.

#include<iostream>
using namespace std;
int main()
{
  // declare variables
  int m, n, count;

  // take input
  cout << "Enter min range: ";
  cin >> m;
  cout << "Enter max range: ";
  cin >> n;
  
  // don't check for -ve numbers
  if(m <= 1) m = 1;
  
  // display result
  cout << "Prime numbers between " 
       << m << " to " << n 
       << " are: " << endl;
  
  for(int i=m; i <= n; i++) 
  {
     count = 0;
     // check for prime
     for (int j=2; j <= i/2; j++)
     {
        if (i % j == 0)
        {
           count++;
           break;
        }
    }
    
    // display result
    if (count == 0)
    cout << i << " ";
  }

  return 0;
}

Output:-

Enter min range: 1
Enter max range: 50
Prime numbers between 1 to 50 are:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Optimized Prime Number Program in C++

In the previous post, C++ program to check prime number we had written a optimized program. Using that program we can develop a C++ program for prime number between 1 to N.

#include<iostream>
#include<math.h>
using namespace std;
int isPrime(int number) ;
int main()
{
    // declare variable
    int m, n;
    int prime;

    // take input
    cout << "Enter min range: ";
    cin >> m;
    cout << "Enter max range: ";
    cin >> n;
    
    // display result
    cout << "Prime numbers between " 
         << m << " to " << n 
         << " are: " << endl;
    
    // loop to repeat the process
    for(int i=m; i<=n; i++) 
    {
        // check prime 
        if(isPrime(i) != 0)
        cout << i << " ";
    }
    
    return 0;
}

int isPrime(int number) 
{

  // negative numbers, 0 and 1 are 
  // not a prime number
  if( number <= 1 ) return 0;

  // 2 and 3 are prime numbers
  if( number <= 3 ) return 1;

  // numbers divisible by 2 and 3
  // are not prime number
  if(number%2==0 || number%3==0)
      return 0;

  // logic for remaining numbers
  for(int i=5; i <= sqrt(number); i=i+6) {

      // 6k+1 => number%i
      // 6k-1 => number % (i+2)
      if(number%i == 0 || number%(i+2) == 0) 
          return 0;
  }

  // if all above conditions are not satisfied
  return 1;
}

Output:-

Enter min range: 1
Enter max range: 100
Prime numbers between 1 to 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Learn More C++ Programming Examples,

Leave a Comment

Your email address will not be published. Required fields are marked *