Navigation Bar

C++ Program To Find Prime Numbers

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.

  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. 

Program to find perfect number

What is a perfect number?
"Perfect number is a positive number which sum of all positive divisors excluding that number.
For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
and  28 is also a Perfect Number
 since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128
CODE FOR PERFECT NUMBER IN C++



  1. #include<iostream.h
  2. #include<conio.h>
  3. void main()                 //Start of main
  4. {
  5.   clrscr();
  6.    int i=1, u=1, sum=0;
  7.    while(i<=500)
  8.  {                                  // start of first loop.
  9.    while(u<=500)
  10.    {                               //start of second loop.
  11.      if(u<i)
  12.      {
  13.       if(i%u==0 )
  14.       sum=sum+u;
  15.      }                          //End of if statement
  16.      u++;
  17.    }                           //End of second loop
  18.    if(sum==i)
  19.    {
  20.     cout<<i<<" is a perfect number."<<"\n";
  21.    }
  22.    i++;
  23.    u=1;  sum=0;
  24.  }                             //End of First loop
  25.    getch();
  26.  }
 Sample output

is a perfect number.
28 is a perfect number.
496 is a perfect number.

Note:
If you want to calculate perfect number within your desire limit simply take a variable and replaced 500 with it.