Home

Find Prime or Composite Number

Posted by Rashid Sharafat on Mar 05 2010

Compilter: Turbo C++ 3.0

Enter a Number and Find whether it is a Prime number or composite. Also program will tell you total number of divisors of composite number.

Output:

I put 8 as a Input and Program tells us that Number is Composite because it has divisors other than 1 and itself.

Prime or Composite

Source Code:

#include<iostream.h>
 #include<conio.h>
 void main()
 {
 clrscr();
 int n,i=2,divisor=0;
 cout<<"Enter a Number to Find Whether it is Prime or Composite ";
 cin>>n;
 while(i<n)
 {
 while(n%i==0)
 {
 i=i+1;
 divisor=divisor+1;
 }
 i=i+1;
 }
 if(divisor==0)
 {
 cout<<"Number is Prime";
 }
 else
 {
 cout<<"Number is Composite"<<endl;
 cout<<"Total Divisors except 1 and itself: "<<divisor;
 }
 getch();
 }