FIND PRIME FACTORS OF A NUMBER USING C PROGRAM






Prime factor of a number in c

#include<stdio.h>
int main(){
  int num,i=1,j,k;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num){
      k=0;
      if(num%i==0){
         j=1;
          while(j<=i){
            if(i%j==0)
                 k++;
             j++;
          }
          if(k==2)
             printf("\n%d is a prime factor",i);
      }
      i++;
   }
   return 0;
}






25 comments:

Unknown said...

int main()
{
int num,i=2;

printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
if(num%i==0)
{
num = num/i;
printf("%d is a prime\n", i );
i=1;
}
i++;
}
system("pause");
return 0;
}

Unknown said...

#include
#include
int prime(int);
void main()
{
int n,i,flag=0;
clrscr();
cout<<"\n enter the number";
cin>>n;
while(n>1) //counting the number of times prime number is being multiplied
{
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
flag=prime(i);
if(flag==0)
cout<<"\n the prime factor is"<<i;
break; //counting the number of times prime number is being multiplied
}
}
n=n/i; //counting the number of times prime number is being multiplied
if(n==1) //counting the number of times prime number is being multiplied
cout<<"\n the prime factor is"<<i; //counting the number of times prime number is being multiplied
}
getch();
}
int prime(int i)
{
int k,result=0;
if(i==2)
return 0;
else
{
for(k=2;k<=i-1;k++)
{
if(i%k==0)
{
result=1;
break;
}
else
result=0;
}
if(result==0)
return 0;
else
return 1;
}
}

Kshitij Gupta said...

//Print only the prime factors.
//Compiler used gcc compiler on DEV-C++ 4.9.9.2 IDE
#include
#include
int main()
{
int num,i,flag,n;
printf("Enter the number whose prime factors you want:");
scanf("%d",&num);
i=2;
n=num;
printf("\nThe Prime factors of %d is/are:",num);
while(i<=num)
{
flag=0;
while(n%i==0)
{
n=n/i;
flag++;
}
if(flag>0)
{
printf("\n%d",i);
}
++i;
}
printf("\n");
system("PAUSE");
return 0;
}

Anonymous said...

can anyone give c code for findng gcd of 2 nos using prome factorization..plz mail to me at bhaveshpurohit18@gmail.com

Mr. devendra tiwari said...

an alternate code for finding prime factor of any number:
#include
int main()
{
long num,i,j,t,s;
printf("enter the number for finding prime factor\n");
scanf("%ld",&num);
for(i=2;i<num;i++)
{
t=num%i;
if(t==0)
{
for(j=2;j<i;j++)
{
s=i%j;
if(s==0)
{
break;
}
}
if(s!=0)
{
printf("prime factor=%ld\n",i);
}
}
}
}

Anonymous said...

Another one
main(){
int num;
printf("Enter the numer : ");
scanf("%d",&num);
primef(num);
printf("\n");
}
primef(int a)
{
int i,temp=a;
for(i=2;i<=temp;i++){
while(temp%i==0){
temp=temp/i;
printf("%d ",i);
}
}
}

Anonymous said...

Can someone help me should a use this code with this problem? Given a number n (n<=10^12) print all its prime factors in strictly ascending order.(Note each prime
factor should be printed on a new line)

సతీష్ said...

#include
int main()
{

int num=100,x;
if(num>=10)
x=num%9;
else
x=num;
printf("%d",x);

return 0;
}

Anonymous said...

#include
int main()
{
int num,i=2,j;
printf ("Enter the number you want to prim factors:");
scanf("%d",&num);
printf("The prim factors are = ");
while(num>1){
for(i=2;i<num;i++){
if(num%i==0){
printf("%d ",i);
break;
}
}
num=num/i;
}

for(j=2;j<=i;j++){
if(i%j==0)
break;
}
if( i==j){
printf("%d",j);
}
getch();
return 0;
}

Anand said...

You can half the number of iterations.

#include
main()
{
int temp, i, flag, num;
printf("enter num ");
scanf("%d",&num);
temp = num/2;
for(i = 2; i <= temp; i++)
{
flag = 0;
while(!(num % i))
{
flag = 1;
num /=i;
}
if(flag)
printf("%d ",i);
}
if(num > 1)
printf("%d ",num);
}

Anonymous said...

can someone specify what i, j, and k represent?

Ios code geeks said...

// find the prime factor using function
#include
#include
void main()
{
int num;
int prime(int num);
int i ,flag=0;
printf("\n enter the any numnber:");
scanf("%d",&num);

for(i=2;i<num;i++)
{

if(num%i==0)
{
flag++;
int res= prime(i);

}

}

if(flag==0)
printf("\n the primefacter is %d",num);


}


int prime(int num)
{
int i, count=0;


for(i=2;i<=num/2;i++)
{
count=0;
if(num%i==0)
{


count=count++;
break;
}


}

if(count==0)
{

printf("\n the prime factor are: %d",num);
return num;

}
}

Sohail Saha said...

Here is a far easier approach. Did nobody think of the simpler approach??

#include
#include
void findfactor(int);
void main()
{
int i;

printf("Enter a positive integer: ");
scanf("%d",&i);
findfactor(i);
printf("\n\nPRESS ANY KEY TO QUIT...");
getch();
}

void findfactor(i_here)
{
int a=2;

for(;a<=i_here;)
{
if((i_here%a)==0)
{
printf(" %d ",a);
i_here=(i_here/a);
}
else
a++;
}
}

Unknown said...

can u please post the output for the code ..... that would be helpful to me..... please
:)

Unknown said...

plz help me to make that code dev c++
my dear freind if you know it plz sand to me this email
axmedyusuf28@gmail.com
topic for c++
Calculate factorial number of any given number

anonymous said...

can u please post the cde for accepting ten digit no.and check wether the last digit contains number 9 ..plzzzz

Unknown said...

can u plzz post the code for reading the elements of an array in process1 and pass it to process2

Pankaj Prakash said...

code need to be efficient as it works well with small numbers but needs a better algorithm for larger numbers.

Unknown said...

Is This Code Works Perfectly??
Please Review The Logic

Saheb MCP said...

output :
Enter a number : 10
prime factors are 2 5
Explonation:
factors of 10 are 1,2,5,10
among them prime numbers are 2,5.
so 2,5 are prime factors of 10

Milan Paudel said...

Can any one make flow chart using for loop

Shaant said...

EASIEST ONE...

#include
#include
void main()
{
int n, i;
clrscr();

printf("Enter any no.\n");
scanf("%d", &n);

for(i = 2; i <= n; i++)
{
if(n % i == 0)
{
printf("%d\t", i);
n = n /i;
--i;
}
}

Unknown said...

int n,p;
clrscr();
printf("\n enter a value=");
scanf("%d",&n);
p=2;
while(n>1)
{
while(n%p==0)
{
printf("%d",p);
n=n/i;
}
p++;
}
getch();


Unknown said...

int i,p;
clrscr();
printf("\n enter a value=");
scanf("%d",&n);
printf("prime numbers are=");
p=2;
while(n>1)
{
while(n%p==0)
{
printf("%d",p);
n=n/i;
}
p++;
getch();






































Unknown said...

int n,p;
clrscr();
printf("\n enter a value=");
scanf("%d",&n);
p=2;
while(n>1)
{
while(n%p==0)
{
printf("%d",p);
n=n/i;
}
p++;
}
getch();