-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime.cpp
More file actions
49 lines (43 loc) · 1.02 KB
/
Copy pathPrime.cpp
File metadata and controls
49 lines (43 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//WAP to print Factors and prime factors of a number
#include <iostream>
using namespace std;
bool checkPrime(int n) //Function to check if prime
{
int ctr=0;
for(int i=1;i<=n/2;i++)
{
if(n%i==0) //Checking for divisibility
ctr++; //Increasing counter if divisible..
}
if(ctr==1)
return true;
else
return false;
}
void printFactors(int n) //Function to print factors
{
for(int i=1;i<=n;i++)
{
if(n%i==0)
cout<<i<<"\t";
}
}
void printPFactors(int n) //Function to print Prime factors
{
for(int i=1;i<=n;i++)
{
if(n%i==0)
if(checkPrime(i)) //Checking for prime
cout<<i<<"\t";
}
}
int main()
{
int num;
cout<<"Enter a number"<<endl;
cin>>num;
cout<<"Factors of "<<num<<" are: ";
printFactors(num); //Calling printFactors()
cout<<"\nPrime Factors of "<<num<<" are: ";
printPFactors(num); //Calling printPFactors()
}