DigitalOrgano

  • Home
  • About
  • Categories
    • Windows OS
    • Software
    • Android
    • Technology
  • Contact
DigitalOrgano » Technology » C++ Program To Find Prime Number

C++ Program To Find Prime Number

Last updated on June 22, 2020 by Editorial Staff Under Technology

Tweet
Share
Share
Pin

Basically, what is Prime number? Prime number is a number that cannot be divided by any other number except by number 1 and by itself.

In this articles we will develop a C++ program to find if the given number is prime or not

So let’s get started

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int hit=0,number,m;
    cout<<"Program to find Prime Number"<<endl;
    cout<<"Enter any number"<<endl;
    cout<<"Number: ";
    cin>>number;
    for (m=1;m<=number;m++)
    {
        if (number%m==0)
        {
            hit++;
        }
    }
    if (hit==2)
    {
        cout<<"The Number is Prime";
    }
    else
    {
        cout<<"The Number is not Prime";
    }
    getch();
    return 0;
}

Explaination

When the Program Run, it will ask for number from the user. When user enter the number, the program will run a for loop and inside the loop there is an if statement that check if the number is divisible by the number in the loop.

If the number is divisible by the number in the loop, the variable hit will get increment.

After the condition in the loop becomes false. It will exit the loop.

In line 18 the if condition will check if hit is equal to 2. If hit is equal to 2. Then the number is a prime number else the number is not a prime number

Compiler Used: CodeBlock

Output

CPP-Program-To-Find-Prime-Number

« C++ Program To Convert Decimal To Hexadecimal
Make Computer Speak Any Entered Words »

Our Social Links

  • facebook
  • instagram
  • feedburner

Latest Articles

  • Create Shortcut For Any Program Or Folder In My Computer
  • The Best Way To Post Story On Instagram Using PC
  • Download Latest Google Chrome Offline Installer
  • 5 Best Ways to Free Up Drive Space on Windows 10
  • How to Enable Dark Mode in Microsoft Office
  • How to Properly Restore Windows Registry in Windows 10
Copyright © 2025 DigitalOrgano