C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Sunday, December 22, 2013

Program Find Prime or Composite Number

Today, we will discuss a C++ Program Find Prime or Composite Number. That is to write a program to Input a number and check whether it is prime number or composite number.

What is a Prime Number? 

A prime number is a whole number greater than 1 that only has two factors or divisors) which are itself and one. For example, 2 is a prime number (actually first prime number), because it has only two divisors, that is, 1 and 2. Similarly, 5 is divisible by 1 and 5 (only two divisors), so 5 is also a prime number.While 6 has divisors 1, 2 ,3 and 6 so 6 is not prime, but 6 is composite number.

C++ Program To Find Prime or COMPOSITE Number

What is a Composite Number? 

A composite number has more factors (or divisors)in addition to one and itself. For example, 8 has positive divisors : 1, 2, 4 and 8. So 8 is composite. 

Note:- The numbers 0 and 1 are neither prime nor composite.

Write a C++ Program to input a positive number, then check whether this number is
a prime number or composite number?


Program Logic For Program Find Prime or Composite Number

  1. First of all we will display a message to enter a number and get a number into a variable called "number".
  2. We will check by IF statement that if enetered number is 0 or 1 then display a message that 0 or 1 are neither prime nor composite. 
  3. Now, we know that a number is always divided by 1 and itself. Also we know that a number can be divided by the numbers between 2 and its mid number, in addition to 1 and itself. For example, Let the number is 8. Now 8 can be divided by numbers 1, 2, 4 and 8. So we will check for divisors 2 to 4(mid of 8).  (Note: We ignore 1 and 8 as it is obviously a divisors of  8). Let the number is 13. Now we should check divisors from 2 to 6 (integer mid of 13).
  4. So we calculate mid = number / 2. 
  5. Let the number is 6. Then mid = 6/2 which gives 3.
  6. Now we will loop to check any divisor present from 2 to(mid) 3. If any number between 2 and 3 divides 6 evenly then we will set a boolean variable Primeflag to 0. Which means 6 is not a prime. Note that we have already initialized the boolean variable to 1. So if there is no divisor between 2 and mid number then Primeflag will never be set to 0.
  7. We will break the loop by using break; statemnt.
  8. Now we check the boolean variable Primeflag. If it is 1 then display Prime message else display a Composite Number message.

 Actual Source Code for Program Find Prime or Composite Number

/*
Write a C++ Program to input a positive
number, then check whether this number is
a prime number or composite number?

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>  // for cout / cin etc

#include<conio.h>   // for clrscr() etc

#include<stdlib.h> // for exit() etc

int main()
{

  clrscr();

  long number, counter, mid;
  bool Primeflag =1;
  cout<<"\nEnter a  number greater than 1 to check \nfor Prime or Composite: ";

  cin>>number;

  if ( number == 0 || number ==1 )
      {
      cout<<"\n 0 and 1 are niether prime nor composite\n";
      cout<<"\n Press any key to continue ... Thanks!";
      getch();
      exit(0);
      }

  mid = number / 2;

  for (counter = 2; counter <= mid; counter++)
        if( number % counter == 0 )
           {
           Primeflag =0;
           break;
           }
  if ( Primeflag == 1)
      cout<<"\n The number "<<number<<" is a Prime Number";
  else
      cout<<"\n The number "<<number<<" is a Composite Number";

  cout<<"\n Press any key to continue ... Thanks!";
  getch();
  return 0;
}

Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Blog Archive

Labels