Copyright © Programs ++
Design by Dzignine
Thursday, 8 March 2012

C++ program to check PRIME numbers

My previous post was about prime numbers and demonstrated a program to generate them in C++ . This   post is about finding whether a number is prime or not. Now as stated earlier, prime numbers are only divisible by the number 1 and themselves, and we will be using this particular property to find whether a number is prime or not. Please read the post Prime number generation in C++ for more information on prime numbers and how to generate them.

The method is simple, divide the prime number by integers ranging from 2 to number-1. If the division generates a whole number(that is remainder is 0), then it is not a prime number, as they are only divisible by themselves(and 1) and not by any other number.

For e.g if we want to check whether 5 is a prime number or not(which it is :p ), we simply divide 5 by numbers less than 5 i.e 2,3,4 . Since the division will not generate a whole number, hence it is a prime number. In the language of coding, simply run a loop from 2 to number-1 and divide it with the number itself. If the remainder is 0 then it is not a prime no. If it is 1 then it is a prime number. Simple !! :)

The following program demonstrates the above method : 

// Program to check whether a given integer is prime or not

#include <iostream>
#include <iomanip> // for exit() function

int main()
{
     int number;
     bool flag = false;
     // prompts the user to enter a value to test for prime property
     std::cout<<" Enter the number to test : ";
     std::cin>>number;

     if ( number == 2 ) // checks if 2 is entered(as 2 is a prime number )
     {
          std::cout<<" The number is PRIME !!! ";
          exit(true); // terminates the program is two is enterd, as there is no need for further checking
     }

     else // divides the number with value from 2 to (number-1)
     {
          for (int i=2; i<number; i++)
          {
               // if it is divisible than values smaller than number-1 it is non - prime
               if ( number%i == 0)
                    flag = false;
          }
     }
     // prints the correct message based on the flag value
     if ( flag == true )
          std::cout<<" \n The number is NON-PRIME !!!";
     else
          std::cout<<"\n The number is PRIME !!! ";

     return 0;
} //end of main
------ OUTPUT ------

 ------ Some Facts ------
  • The only even prime number is 2. All other even numbers can be divided by 2.
  • If the sum of a number's digits is a multiple of 3, that number can be divided by 3.
  • No prime number greater than 5 ends in a 5. Any number greater than 5 that ends in a 5 can be divided by 5.
  • Zero and 1 are not considered prime numbers.
  • Except for 0 and 1, a number is either a prime number or a composite number. A composite number is defined as any number, greater than 1, that is not prime.

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.  

------ Related Posts ------
 

2 comments:

  1. In all types of computer programming languages,prime numbers are used to make programs.Its easy to generate even numbers but to generate odd and prime numbers are little bit tricky.

    ReplyDelete
  2. This code is frequently ask in any examination Check Number is Prime or not in C++ is very simple and easy to write. Using for loop and if else we can write this code. Your code is very simple and easy to understand. Thanks for sharing this article.

    ReplyDelete

Comment Here....