Featured
- Get link
- X
- Other Apps
Prime Factors Program in C++
//Following Program displays Prime Factors of a given number
#include<iostream.h>
#include<conio.h>
int ifprime(int a)
{
for(int i=2;i<=a/2;i++)
{
if(a%i==0)
return 0;
}
return 1;
}
void factors(int n)
{ int s=n;
int arr[50],len=-1,st;
for(int a=2;a<n;a++)
{ n=s;
st=ifprime(a);
if(st==1 && n%a==0)
{
while(n%a==0)
{
arr[++len]=a;
n=n/a;
}
}
}
len++;
cout<<"=";
for(int i=0;i<len;i++)
{
cout<<arr[i]<<"x";
}
}
void main()
{
clrscr();
int n;
cout<<"Please enter a number to generate prime factors\n";
cin>>n;
factors(n);
getch();
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
int ifprime(int a)
{
for(int i=2;i<=a/2;i++)
{
if(a%i==0)
return 0;
}
return 1;
}
void factors(int n)
{ int s=n;
int arr[50],len=-1,st;
for(int a=2;a<n;a++)
{ n=s;
st=ifprime(a);
if(st==1 && n%a==0)
{
while(n%a==0)
{
arr[++len]=a;
n=n/a;
}
}
}
len++;
cout<<"=";
for(int i=0;i<len;i++)
{
cout<<arr[i]<<"x";
}
}
void main()
{
clrscr();
int n;
cout<<"Please enter a number to generate prime factors\n";
cin>>n;
factors(n);
getch();
}
OUTPUT:
Prime Factors Program in C++ |
- Get link
- X
- Other Apps
Labels:
C++ program to find prime factors of a number
C++ Program to print prime factors
prime factors program in C++
Popular Posts
C++ program to display numbers which get reversed after multiplying by 4
- Get link
- X
- Other Apps
Comments
Post a Comment