Q:

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

belongs to collection: C++ Basic Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to find Factorial of a Number using Re... >>
<< C++ Program to Check whether given number is Even ...