-->





FIND PRIME NUMBER IN C++

What is a PRIME NUMBER?
" A Natural number greater than 1 which has only two divisor 1 and itself is called prime number ".
For Example:  
5 is prime, because it has only two divisors 1 and itself.

C++ Program To Find Prime Numbers


  1. #include<iostream.h>
  2. #include<conio.h>
  3.         void main()
  4.         {
  5.          //clrscr();
  6.          int number,count=0;
  7. cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
  8.           cin>>number;
  9.            for(int a=1;a<=number;a++)
  10.                {
  11.                 if(number%a==0)
  12.                    {
  13.                   count++;
  14.                    }
  15.                }
  16.        if(count==2)
  17.          {
  18.           cout<<" PRIME NUMBER \n";
  19.          }
  20.        else
  21.          {
  22.           cout<<" NOT A PRIME NUMBER \n";
  23.          }
  24.        //getch();
  25.        }



UNDERSTANDING PRIME NUMBERS PROGRAM LOGIC
  •      To make logic first think about PRIME NUMBER definition that a number which divides by 1 and itself.
  •    So we have make a condition like that in which user will enter a number and our program will check it by dividing it from 1 up to itself.
  •    To check that how much times it has divided to numbers from 1 to itself we take a variable and increment it each times when a number is divided.
  •    In C++ CODING we take a FOR LOOP which will start from 1 up to number that has entered to check whether it is PRIME NUMBER or not with in FOR LOOP we set an IF condition and placed counter (count++) variable in its body so whenever a number from 1 to number which has entered to check divides than IF condition becomes true and counter variable will be incremented.
  •    When FOR LOOP is completed we check from IF condition that if counter variables value is equal to 2 than number is PRIME else NUMBER IS NOT PRIME.
  •    Because if number divided two times by 1 and itself counter variable will incremented two times if more than two times counter variable will have value greater than 2. 
Prime number program image view
Find Prime Number in C++ Programming
C++ program to find prime numbers 
Sample Output:
C++ Programming prime number output sample

More programs

Find factorial of a number in C++
Find Fibonacci Series in C++
C++ Program to find last prime number before entered number 
Swap two variables with out using third variable 
Find binary value of decimal in C++
Find Palindrome Number in C++
Recommended: For fast learning change the logic and do experiment with codes.

52 comments:

  1. it is an complicated program

    ReplyDelete
    Replies
    1. no way man it is very easy to understand.
      Simple logic:if number %(remainder when a division is performed) anything less than the number is 0(it is completely divisible) then the number is prime else not.

      Delete
  2. nice broooooo you are great thanks a lot ..

    ReplyDelete
    Replies
    1. You're*
      I apologize for my grammar Nazi attack.
      You have a very good program there, though.
      Thank you.

      Delete
  3. very easy and comprehensive really helpful

    ReplyDelete
  4. thanks man!!!!!!!!
    please post all basic and important program bro
    please!!!!!!!!!!!!!!!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. It's not a difficult to understand this source but it's difficult for wath it's do..
    Because a positive integer is always divisible by one and by himself you don't need to check again for it.

    The source can be reduce to:
    #include

    using namespace std;

    int main()
    {
    int number;
    cout << "Enter number to check it is prime or not ";
    cin >> number;

    for(int denominator = 2; denominator < number; denominator++)
    {
    if(number % denominator == 0)
    {
    cout << " NOT A PRIME NUMBER \n";
    return 0;
    }
    }

    cout << " PRIME NUMBER \n";
    return 0;
    }

    ReplyDelete
    Replies
    1. #include

      using namespace std;

      int main()
      {
      int number;
      cout << "Enter number to check it is prime or not ";
      cin >> number;

      for(int denominator = 2; denominator < number/2; denominator++)//chk condition to half
      {
      if(number % denominator == 0)
      {
      cout << " NOT A PRIME NUMBER \n";
      return 0;
      }
      }

      cout << " PRIME NUMBER \n";
      return 0;
      }

      Delete
    2. intialize array of 10 *10 with random values matrix and print prime numbers/c++ ?
      do u know its code?

      Delete
  7. This way better:

    for (int i = a; a<=(number+1)/2; ++a) {
    if (number%a == 0) count++;
    }

    ReplyDelete
  8. here assigning a=2 will be better I guess and count==1
    1.#include
    2.#include
    3. void main()
    4. {
    5. //clrscr();
    6. int number,count=0;
    7. cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
    8. cin>>number;
    9. for(int a=2;a<=number;a++)
    10. {
    11. if(number%a==0)
    12. {
    13. count++;
    14. }
    15. }
    16. if(count==1)
    17. {
    18. cout<<" PRIME NUMBER \n";
    19. }
    20. else
    21. {
    22. cout<<" NOT A PRIME NUMBER \n";
    23. }
    24. //getch();
    25. }

    ReplyDelete
    Replies
    1. Yes. Your Solution Is Also Right..

      Delete
    2. But this program will even show the number 1 as prime..... Check it.

      Delete
    3. 1 is prime number dear

      Delete
  9. Boss, please give my programming exam :(

    ReplyDelete
  10. thank you so much really very helpfull...................

    ReplyDelete
    Replies
    1. how i improve my programming logic?

      Delete
    2. To Improve Logic Making Skill In Programming:
      Try To Do a Simple Program In More Than One Ways.
      First Design A Pseudo Code On A Paper Than Code It.
      Some Times Our Mind Does Not Allow Us To Think Complicated Programs We Think Initially And About Logic And If We Do Not Get It Right We Give Up Early..
      Like If Want To Make A Complex Code We Make A Weak Logic And Than Apply Guesses On Code and Some Times It Works To Make The Program But It Is Not A Good Practice To Make The Logic If You Code A Program Than Try To Understand It 100%

      This Skill Will Improve Gradually So Keep Sticking and Never Give Up :)
      Let Me Tell You Will Enjoy Programming Once You Improve Your Logic Making Skill. Happy Coding.. :)

      Delete
  11. thank u for this.. U SAVE SOMEONE FROM GETTING FAILED...

    ReplyDelete
  12. void main()
    {
    int num,a=2;
    cout<<"Enter the num";
    cin>>num;
    while(num%a!=0)
    a++;
    if(num==a)
    cout<<"Prime No.";
    else
    cout<<"NOt Prime ";
    getch();
    }

    ReplyDelete
  13. void main()
    {
    int num,a=2;
    cout<<"Enter the num";
    cin>>num;
    while(num%a!=0)
    a++;
    if(num==a)
    cout<<"Prime No.";
    else
    cout<<"NOt Prime ";
    getch();
    }

    ReplyDelete
  14. Fahad bhai ..
    i made it very easy and it works but have few problems while making it then i changed it.


    #include
    #include
    using namespace std;
    int main()
    {
    int a,c;
    cout<<"Plz enter a number :";
    cin>>a;

    c=0;
    for(int b=1; b<a; b++)
    {
    if(a%b==0)

    c=c+1;

    }

    if(c==1)
    cout<<"prime";
    else
    cout<<"NOt a prime";
    getch();
    }

    problem was when i was using c=c++ instead of c+1 and it wasn't incrementing ..every time it gave me 0 0 0 0..and hence error came

    ReplyDelete
    Replies
    1. Ismail First I appreciates your efforts about program. Keep the hard work going.
      I will suggest you use only c++; instead of c=c++;
      Because
      c++ -> c=c+1;

      Delete
    2. What do you mean by " c++ -> c=c+1; " ???

      Delete
  15. This comment has been removed by the author.

    ReplyDelete
  16. Your programme n explanation along with output made me understand clearly...thans BRO

    ReplyDelete
  17. i have to print a prime number till the value user provide i guess i can do this as
    #include
    using namespace std;
    int main()
    {
    int n;
    cout<<"enter the end limit of prime number:";
    cin>>n;
    for(int i=1;i<=n;)
    {
    if(n<=0)
    {break;}
    if(i/1==1 && i/i==1)
    cout<<i<<endl;
    i++;
    }
    system("pause");
    return 0;
    }

    But its not working any help Please

    ReplyDelete
  18. How would you loop this program so that the user could keep inputting numbers to check whether or not they are prime?

    ReplyDelete
  19. How would you loop this program so that the user could keep inputting numbers to check whether or not they are prime?

    ReplyDelete
  20. hey guys can anyone write swedeb flag ? with cin and while loops ? fast pls

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. This code is correct ?Prime Number Program in C

    #include
    #include
    void main()
    {
    int num,res=0;
    clrscr();
    printf("\nENTER A NUMBER: ");
    scanf("%d",&num);
    res=prime(num);
    if(res==0)
    printf("\n%d is a prime number",num);
    else
    printf("\n%d is not a prime number",num);
    getch();
    }
    int prime(int n)
    {
    int i;
    for(i=2;i<=n/2;i++)
    {
    if(n%i!=0)
    continue;
    else
    return 1;
    }
    return 0;
    }

    ReplyDelete
  24. enter the value 2 then it will show you that 2 is prime number.But 2 is not a prime number so make correction in your program.

    ReplyDelete
  25. Nice try, but the program doesn't consider the numbers '1' and '2'. These two numbers are not primes.

    ReplyDelete