Write a C++ Program to Check a number is Prime or not

By | 25.12.2016

Check a number is Prime or not


Write a C++ Program to Check given number is Prime number or not. Here’s simple C++ Program to Check given number is Prime number or not in C++ Programming Language.


A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. It means a prime number is only divisible by 1 and itself, and it start from 2. The smallest prime number is 2

Here is source code of the C++ Program to Check given number is Prime number or not. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/*  C++ Program to Check given number is Prime number or not  */

#include<iostream>
using namespace std;

int main()
{
  int i,n;

  cout<<"Enter any positive number :: ";
  cin>>n;

  if(n==1)
  {
    cout<<"\nSmallest prime number is :: 2";
  }

  for(i=2;i<n;i++)
  {
      if(n%i==0)
      {
          cout<<"\nThe Entered Number [ "<<n<<" ] is NOT a prime number.\n";
          break;
      }
  }

 if(n==i)
 {
    cout<<"\nThe Entered Number [ "<<n<<" ] is a prime number.\n";
 }

  return 0;
}

Output : :


/*  C++ Program to Check given number is Prime number or not  */

Enter any positive number :: 97

The Entered Number [ 97 ] is a prime number.

Process returned 0

Above is the source code for C++ Program to Check given number is Prime number or not which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.5 4 votes
Article Rating
Subscribe
Notify of
guest
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ahmedseid

if i=10 and n=10 i,e n=i, is n(10)prime?

codezclub  Post author

but it’s not possible,Since we create a condition ::
if(n%i==0)

then break;

Correct code

#include <iostream>

using namespace std;

int n;
bool isnum = true;

int main()
{
cout << “enter the number ” << endl;
cin >> n;
if (n == 0 || n == 1)
{
  isnum = false;
}
else{
  for (int i=2; i <= n/2; ++i) {
    if (n%i == 0) {
      isnum = false;
      break;
    }
  }
}
if (isnum) {
  cout << “number is prime ” << endl;
   
}
else { 
  cout << “number is not prime ” << endl;
}

  return 0;
}

Alexis Siobhan Douglas

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout << “Enter a number:: “;
int num;
cin >> num;

if (num > 0) {
if ((num % 2 == 0) || (num % 3 == 0))
cout << “Not a Prime Number”;
else if ((num % 2 != 0) || (num % 3 != 0))
cout << ” Is a prime number”;
else
cout << “Invalid”;

}

return 0;
  }

// Prime numbers: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 61 67 71 73 79 83 89 97

Nabeel Tiwana

totally wrong condition because 1 is not prime then 2 is prime and 3 is prime so test your code

image_2022-08-14_013751209.png
Nabeel Tiwana

//Write a C++ Program to Check a number is Prime or not     // Prime numbers: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 61 67 71 73 79 83 89 97     #include <iostream>     #include <cmath>     using namespace std;     int main()     {       int num;     cout << “Enter a number “;     cin >> num;     if (num <=1)     {       cout<<“prime number is start from 2”;     }     else if(num==2 || num==3)     {             cout<<“prime numbr”;     }     else if ((num % 2 == 0) || (num % 3 == 0))     {       cout << “Not a Prime Number”;     }… Read more »

Last edited 2 years ago by Nabeel Tiwana
Ajay Waghmare

Great I like your website. You solved all the problems of a college student now they will not build their own algorithm and will fail in interview.

Faisal

Mind Blowing