The factors which are prime for a number are its Prime Factors.It does sound easy but calculating them by a program is little tricky.

The logic i used is that a number when divided by starting from 2 and keep diving until its not possible then go to next number i.e 3 and start dividing else go to next number i.e 4 but its not possible as its not prime so go to next number and so on.

The output format is done with the sign ^ which means raised to. So if its like 2^4 , this means 2 is raised 4 times..i.e 2*2*2*2=16

Source Code:

#include <iostream>

#include <math.h>

#include <malloc.h>

using namespace std;

int main()

{

int n;

cout<<"**Compute Prime Factors of a number**\nEnter a number:";

cin>>n;

cout<<"Prime Factors=\n";

for(int i=2;i<=sqrt(n);i++){

int count=0;

while(n%i==0){

n=n/i;

count++;

}

if(count!=0) cout<<i<<"^"<<count<<" ";

}

if(n!=1)

cout<<n<<"^1";

cout<<"\n\n";

return 0;

}

The logic i used is that a number when divided by starting from 2 and keep diving until its not possible then go to next number i.e 3 and start dividing else go to next number i.e 4 but its not possible as its not prime so go to next number and so on.

The output format is done with the sign ^ which means raised to. So if its like 2^4 , this means 2 is raised 4 times..i.e 2*2*2*2=16

Source Code:

#include <iostream>

#include <math.h>

#include <malloc.h>

using namespace std;

int main()

{

int n;

cout<<"**Compute Prime Factors of a number**\nEnter a number:";

cin>>n;

cout<<"Prime Factors=\n";

for(int i=2;i<=sqrt(n);i++){

int count=0;

while(n%i==0){

n=n/i;

count++;

}

if(count!=0) cout<<i<<"^"<<count<<" ";

}

if(n!=1)

cout<<n<<"^1";

cout<<"\n\n";

return 0;

}