Hi guys I have some problem regarding getting prime factors and distinct prime factors.
I manged to get the prime factors but I got stuck on how to get the distinct prime factors.........I don't know how to select unique prime factors and print them....Help me out guys........

Thank you in advance....

My code to get just the prime numbers is............

#include<stdio.h> 
#include<conio.h> 

int main() 
{ 
int n,i; 
printf("Enter a Number:"); 
scanf("%d",&n); 
printf("\n\nPrime Factors of %d is: ",n); 
for(i=2;i<=n;i++) 
{ 
if(n%i==0) 
{ 
printf("%d,",i); 
n=n/i; 
i--; 
if(n==1) 
break; 
} 
} 
getche(); 
}

Recommended Answers

All 6 Replies

You need to format your code before posting...

No idea what you are asking. Distinct prime factors? Of what? Show us an example...

I dont know what u r asking but a liked your algo for finding prime factors.

Explain your query in a little more detail.

Hi guys I have some problem regarding getting prime factors and distinct prime factors.
I manged to get the prime factors but I got stuck on how to get the distinct prime factors.........I don't know how to select unique prime factors and print them....Help me out guys........

Thank you in advance....

My code to get just the prime numbers is............

#include<stdio.h> 
#include<conio.h> 

int main() 
{ 
int n,i; 
printf("Enter a Number:"); 
scanf("%d",&n); 
printf("\n\nPrime Factors of %d is: ",n); 
for(i=2;i<=n;i++) 
{ 
if(n%i==0) 
{ 
printf("%d,",i); 
n=n/i; 
i--; 
if(n==1) 
break; 
} 
} 
getche(); 
}

unique prime factor
#include<stdio.h>
void main()
{

int n,i,q,u;
printf("enter no to find factor");
scanf("%d",&n);
for(i=2;i<n;i++)
{

if(n%i==0)
{
if(u!=i){
printf("\t%d",i);
u=i;
}

n=n/i;
i--;
if(n==1)

}
}

}

Hi guys I have some problem regarding getting prime factors and distinct prime factors.
I manged to get the prime factors but I got stuck on how to get the distinct prime factors.........I don't know how to select unique prime factors and print them....Help me out guys........

Thank you in advance....

My code to get just the prime numbers is............

#include<stdio.h> 
#include<conio.h> 

int main() 
{ 
int n,i; 
printf("Enter a Number:"); 
scanf("%d",&n); 
printf("\n\nPrime Factors of %d is: ",n); 
for(i=2;i<=n;i++) 
{ 
if(n%i==0) 
{ 
printf("%d,",i); 
n=n/i; 
i--; 
if(n==1) 
break; 
} 
} 
getche(); 
}

unique prime factor
#include<stdio.h>
void main()
{

int n,i,q,u;
printf("enter no to find factor");
scanf("%d",&n);
for(i=2;i<n;i++)
{

if(n%i==0)
{
if(u!=i){
printf("\t%d",i);
u=i;
}

n=n/i;
i--;
if(n==1)
break;
}
}

}

what exactly do you mean by distinct prime factor?

//Compiler Turbo C++ IDE
//If you give Prime No as an ip it will show the result like "No is Prime"
//With Non Prime Number It will Show Desired Result

//For further Doubt mail me @ [removed]

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void primefactors(int);
int pf=0;
void main()
{
int number;
int i,j,ct=0;
clrscr();
printf("Enter the Number:");
scanf("%d", &number);

if(number==2){
printf("Number is Prime and Prime Factor is %d",number);
exit(0);
}

for(i=2;i<=number;i++)
{
if(number%i==0 && i<number)
{
ct++;
printf("Number is not prime\n");
primefactors(number);
break;
}

if(number%i==0 && ct==0){
printf("No is Prime");
break;
}




}
//return 0;
getch();

}

void primefactors(int number)
{
int i,k=0;
int ct;
static int j;
int p[1000][2];

j=number;


printf("Prime factors are\n");
for(i=2;i<=number;i++)
{  ct=1;
   while(j%i==0)
   {
  // ct=ct+1;
   printf("%d ",i);
    // printf("*");
  // j=j/i;

   p[k][0]=i;
   p[k][1]=ct;
   ct=ct+1;
   j=j/i;
// printf("%d\n",p[k][0]);
// printf("%d\n",p[k][1]);


}
 if(ct!=1 && j%i!=0)
 {
 k++;
 }


}
printf("\n\n");

for(i=0;i<k;i++)
{
printf("%d %d ",p[i][0],p[i][1]);
printf("\n");
pf=pf+p[i][1];
}

printf("Total No of prime Factors are:%d\n",pf);
printf("Distinct No. prime factors are:%d",k);

}
commented: Do NOT give solutions to problems. Students don't learn if you do their homework for them. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.