This article contains C++
program to find prime factors of a number.
Steps:
C++ Program to Find Prime Factors of a
Number
(1) Read a positive integer x (using
cin>>x) and initialize a variable i to 2.
(2) While x is divisible by i, print i and
divide x by i.
(3) After step 2, increment i.
(4) Repeat step 3 and 4, till x>1.
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a
positive integer : ";
cin>>x;
cout<<endl<<"Prime factors of "<<x<<"
are : "<<endl;
for(int i=2;x>1;i++)
{
while(x%i==0)
{
cout<<i;
x=x/i;
cout<<"\t";
}
}
return 0;
}
OUTPUT 1:
Enter a positive integer : 150
Prime factors of 150 are :
2 3
5 5
OUTPUT 2:
Enter a positive integer : 36
Prime factors of 36 are :
2 2
3 3
OUTPUT 3:
Enter a positive integer : 63
Prime factors of 63 are :
3 3
7
0 comments:
Post a Comment