What is a

Sample Output:

**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**

- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- //clrscr();
- int number,count=0;
- cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
- cin>>number;
- for(int a=1;a<=number;a++)
- {
- if(number%a==0)
- {
- count++;
- }
- }
- if(count==2)
- {
- cout<<" PRIME NUMBER \n";
- }
- else
- {
- cout<<" NOT A PRIME NUMBER \n";
- }
- //getch();
- }

**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.

**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.**
Thanks...

ReplyDeleteYour Welcome.

DeleteThis comment has been removed by the author.

DeleteWelCome...

ReplyDeleteit is an complicated program

ReplyDeleteno way man it is very easy to understand.

DeleteSimple 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.

nice broooooo you are great thanks a lot ..

ReplyDeleteYour Welcome Brother.

DeleteYou're*

DeleteI apologize for my grammar Nazi attack.

You have a very good program there, though.

Thank you.

Its really easy to understand.

ReplyDeletevery easy and comprehensive really helpful

ReplyDeletethanks man!!!!!!!!

ReplyDeleteplease post all basic and important program bro

please!!!!!!!!!!!!!!!

quite helpful..............

ReplyDeleteThis comment has been removed by the author.

ReplyDeleteIt's not a difficult to understand this source but it's difficult for wath it's do..

ReplyDeleteBecause 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;

}

#include

Deleteusing 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;

}

intialize array of 10 *10 with random values matrix and print prime numbers/c++ ?

Deletedo u know its code?

This way better:

ReplyDeletefor (int i = a; a<=(number+1)/2; ++a) {

if (number%a == 0) count++;

}

here assigning a=2 will be better I guess and count==1

ReplyDelete1.#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. }

Yes. Your Solution Is Also Right..

DeleteBut this program will even show the number 1 as prime..... Check it.

Delete1 is prime number dear

DeleteFind Fibonacci Series In C++

ReplyDeleteBoss, please give my programming exam :(

ReplyDeletethank you so much really very helpfull...................

ReplyDeleteWelcome Faiza Aslam.

Deletehow i improve my programming logic?

DeleteTo Improve Logic Making Skill In Programming:

DeleteTry 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.. :)

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

ReplyDeletevoid main()

ReplyDelete{

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();

}

void main()

ReplyDelete{

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();

}

Fahad bhai ..

ReplyDeletei 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

Ismail First I appreciates your efforts about program. Keep the hard work going.

DeleteI will suggest you use only c++; instead of c=c++;

Because

c++ -> c=c+1;

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

DeleteThis comment has been removed by the author.

ReplyDeleteYour programme n explanation along with output made me understand clearly...thans BRO

ReplyDeletei have to print a prime number till the value user provide i guess i can do this as

ReplyDelete#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

very very thank,s Bro!!!!

ReplyDeleteBEST BLOG FOR BIGGNERS

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

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

ReplyDeletethumbs up!!

ReplyDeletegud

ReplyDeletePrime number program in C++

ReplyDeleteThanks nice code

nice code bro. keep sharing

ReplyDeletenice code bro. keep sharing

ReplyDeletehey guys can anyone write swedeb flag ? with cin and while loops ? fast pls

ReplyDeleteThis comment has been removed by the author.

ReplyDeleteThis code is correct ?Prime Number Program in C

ReplyDelete#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;

}

Prime numbers are a special kind of natural numbers or positive integers which are exactly divisible by 1 and the number itself or such as 2,3,5,7 and 11.

ReplyDeletePrime numbers can only be divided into integers.Aside from divide and count test and various sieving methods, it is believed that Prime Number Distribution Series (PNDS) is the mathematical formula for finding prime numbers.

ReplyDeleteenter 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.

ReplyDeleteprime number distribution . (PNDS), developed by the NSI team of mathematicians2 which computes the exact count of primes numbers.

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

ReplyDelete