Thursday 5 July 2012

Program to display prime factor of inputed number in c++ .

#include<iostream>
using namespace std;

void factor(int );

int main()
{
int k;
cout<<"Enter your number:";
cin>>k;
cout<<endl;
factor(k);
return 0;
}



    void factor(int a)
    {
    int j,n,i,flag,k,temp;
        flag=0;
         j=a;
        cout<<"prime factors :";
        for(i=2;i<j;i++)   
        {
        temp=0;
        for(k=2;k<i;k++)   
        {
           if(i%k==0)
            {
            temp=1;
            }
        }
        if(temp==0);
        {
        while(a%i==0)
            {
            a=a/i;
            cout<<i<<"     ";
            flag=1;
            }
        }   
        }
        if(flag==0)
        cout <<"   not available";
    return ;
 }

No comments:

Post a Comment