Determination of Prime Factors using Functions.

A positive integer is entered through the keyboard.
Write a function to obtain the prime factors of this number.
For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.



#include<stdio.h>
main()
{

int number;
int prime(int number);

int primefactor(int number);


printf("Enter the number whose prime factors are to be calculated:");

scanf ("%d", &number);

primefactor(number);

}

//The following function detects a Prime number.

prime(int num)
{
int i, ifprime;


for (i=2; i<=num-1; i++)

{
if (num%i==0)
{

ifprime=0;
}
else
ifprime=1;

}
return (ifprime);
}


//The following function prints the prime factors of a number.

primefactor(int num)
{
int factor,ifprime;
for (factor=2; factor<=num;)

{
prime(factor); //so that the factors are only prime and nothing else.
if (ifprime)

{
if (num%factor==0) //diving by all the prime numbers less than the number itself.
{

printf("%d ", factor);
num=num/factor;

continue;
}
else
{
factor++;//this cannot be made a part of the for loop

}
}
}
return 0;
}





The file can be found at:
Download File

Comments

bbb said…
Another method using recursion:

void main()
{
int x;
printf("\nInput an integer\n");
scanf("%d",&x);
prime(x);
}

prime(int x)
{
int a;
for(a=2;a<=x;a++)
{
if(x%a==0)
{
printf("%d ",a);
prime(x/a);
break;
}
}
}
------------------------
jelly.fishinq@gmail.com
bbb said…
without recursion

void main()
{
int x;
printf("\nInput an integer\n");
scanf("%d",&x);
prime(x);
}

prime(int x)
{
int a;

for(a=2;a<=x;a++)
{
if(x%a==0)
{
printf("%d ",a);
x/=a;
a--;
}
}
}
surya said…
become a follower for free C,C++ And vb code of my blog:
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.
Anonymous said…
bing chang good and short way
Anonymous said…
Printing prime factors [Recursion method]


#include
#include
int pdetect (int x);
void primefactprint (int x);
void main()
{
int a;
clrscr();
printf("Enter positive integer to obtain its prime factors: ");
scanf("%d",&a);
printf("\nCalling function to obtain prime factors\n");
primefactprint(a);
printf("\nPress any key to exit");
getch();
}

int pdetect (int x)
{
int i,sum=0;
for (i=1;i<=x;i++)
{
if (x%i==0)
{
sum++;
}
}
if (sum<=2)
{
return 1;
}
else
{
return 0;
}
}

void primefactprint (int x)
{
int j,primecondition;
for (j=1;j<=x;j++)
{
primecondition=pdetect(j);
if (primecondition==1)
{
if (x%j==0)
{
printf("\n%d",j);
}
}
}
}
sumathi said…
#include
int pf(int x);
main()
{
int num, count;
printf("Enter the no\n");
scanf("%d",&num);
for (count=1;count<=num;count++)
{
if (num%count==0)
{
int prmfctr=pf(count);
}
}
return 0;
}
int pf(int x)
{
int c;
for (c=2;c<x;c++)
{
int pf=x%c;
if(pf==0)
goto step1;
}
printf("%d is a prime factor\n",x);
step1: printf("\n");
return (pf);
}

Popular posts from this blog

C Program - Calculation of Area and Circumference of a Circle using Pointers

Matchstick Game using C Programming

Generation of Fibonacci Sequence using Recursion.