Prime or Not

Write a program that reads a number and then find if the number is prime or not. If the number is not prime, print all the factors.

#include<iostream>
using namespace std;
void factors(int a){
	for(int i=1;i<=a;i++){
		if(a%i==0)cout<<" "<<i;
	}
}
int main(){
	int a;
	cout<<"Enter a positive number : ";
	cin>>a;
	int count=0;
	for(int i=1;i<=a/2;i++){
		if(a%i==0)count++;
	}
	if(count==1)cout<<"Prime"<<endl;
	else cout<<"Not Prime"<<endl;
	if(count>1)
	{
		cout<<"The factors of "<<a<<" are ";
		factors(a);
	}
	return 0;
}
output of the above program, with factors of 45
output of the above program, with factors of 45
output displaying 101 as prime
output displaying 101 as prime

One Reply to “Prime or Not”

Leave a Reply

Your email address will not be published. Required fields are marked *