Skip to main content

Featured

functions in Python

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:

Prime Factors Program in C++
 

Comments

Popular Posts