Here we will write a C++ program to check whether the given number prime or composite number, we all know what is a Prime number and what is Composite number still, lets recall it quickly
Prime Number: A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).
Composite Number: A number that can be formed by multiplying together two smaller positive integers. Equivalently, it is a positive integer that has at least one divisor other than 1 and itself. For e.g. 14,15, 18 etc.
In short a number that is not prime is a composite number.
How program works:
- This program takes in an integer num1 as a screen input from the user.
- It then determines whether the integer is prime or composite.
- It finally outputs the appropriate message by writing to the ‘cout’ stream.
PROGRAM:
#include<iostream> using namespace std; int main() { int number,count=0; cout<<"Enter a number: "; cin>>number; for(int a=1;a<=number;a++) { if(number%a==0) count++; } if(count==2) cout<<"\t"<<number<<" IS A PRIME NUMBER \n"; else cout<<"\t"<<number<<" IS A COMPOSITE NUMBER \n"; return 0; }