Write a program to generate prime factors of a given integer?
Answers were Sorted based on User's Feedback
Answer / rajesh kumar s
int main()
{
int n,num,i;
printf("enter the num");
scanf("%d",&num);
n=num;
printf("\n");
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
prime(i);
}
}
}
void prime(int x)
{
int i,f=0;
for(i=2;i<=x/2;i++)
{
if(x%i==0)
{
f=1;
break;
}
}
if(f==0)
printf(" %d",x);
}
Is This Answer Correct ? | 49 Yes | 40 No |
Answer / priyanka chauhan
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the no: ");
scanf("%d",&n);
for(j=2;j<=n;j++)
{
if(n%j==0)
{
for(i=2;i<=j-1;i++)
{
if(j%i==0)
break;
}
if(i==j)
printf("%d ",j);
}
}
getch();
}
Is This Answer Correct ? | 23 Yes | 18 No |
Answer / sourav das
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l,n;
clrscr();
printf("Enter The Number:");
scanf("%d",&n);
i=1;
while(i<=n)
{
if(n%i==0)
{
j=1;
k=0;
l=1;
while(l<=j)
{
if(j%l==0)
k++;
l++;
}
if(k==2)
{
printf("\n%d is the prime factor of %d",l-1,n);
}
}
i++;
}
getch();
}
Is This Answer Correct ? | 26 Yes | 24 No |
Answer / shruthi chandran
voiid primeFactors(int n)
{
while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
if (n > 2)
printf ("%d ", n);
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / kalee
Pseudo code: Algorithm ....
If N is the integer, then, any number greater than sqrt(N) will not be a factor of that integer...
so it is enough to check till sqrt(N) integers, that is it is divisible or not... further, if N is odd... forget all the even integers, as they cannot be a part of factors.. :)
Happy coding...
Is This Answer Correct ? | 6 Yes | 9 No |
Answer / varshil shah
#include<stdio.h>
#include<conio.h>
void factors(int);
void main()
{
int num,fact,i;
clrscr();
printf("\n Enter a number :::");
scanf("%d",&num);
if(num==2)
{
factors(num);
}
else
{
for(i=2;i<=num;i++)
{
if(num%i==0)
{
factors(i);
}
}
}
getch();
}
void factors(int n)
{
int i,notprime=0;
if(n==2)
{
printf("\n Prime factor is 2");
}
else
{
for(i=2;i<n;i++)
{
if(n%i==0)
{
notprime++;
}
}
if(notprime==0)
{
printf("\n Prime factor is %d",i);
}
}
}
Is This Answer Correct ? | 5 Yes | 8 No |
Answer / bibek
/*Program To Generate The Prime Factors Of An Entered
Numbers*/
/*Header Files*/
#include<stdio.h>
/*Function Prototypes*/
void factorcheck(long unsigned int prime,long unsigned int
num );
void primefind(long unsigned int num, long unsigned int
startnum);
void main()
{
long unsigned int num;
printf("\n Enter A Positive Number:");
scanf("%d", &num);
printf("\n ");
/*Call To the primefind() function*/
primefind(num,
1); /*Find
a prime less than the given number*/
if (num==0)
{
printf(" 0 \n \n");
}
else if (num==1)
{
printf(" 1 \n \n");
}
else
{
printf("\b \n
\n "); /*Delete the
extra 'x' left in the end*/
}
}
void primefind(long unsigned int num, long unsigned int
startnum)
{
long unsigned int counter1, counter2, primecheck;
char primestat;
for(counter1 = startnum; counter1<=num; counter1++)
{
for (counter2 = 2; counter2< counter1; counter2++)
{
primecheck = counter1 %
counter2; /* Check If The Chosen Number
(counter1) is divisible
by numbers less than it excluding 1*/
if (primecheck != 0 )
primestat
= 't'; /*The Number Is
Prime*/
else /*(So
Far)*/
{
primestat
= 'f'; /*The Number Is
Not Prime*/
break;
}
}
if (primestat =='t' || counter1 ==2)
{
factorcheck
(counter1,num); /*This Calls
The Function factorcheck()*/
break;
/* which checks if the prime number */
}
/*generated is a factor of the given
number*/
}
}
void factorcheck(long unsigned int prime, long unsigned int
num)
{
long unsigned int remainder;
remainder = num%prime;
if (remainder ==
0) /*since the remainder is 0,
the prime number is a factor*/
{
printf("%dx", prime);
primefind((num/prime), 1); /*Find
Another prime number*/
return;
}
else
primefind(num, prime+1); /*Since
The Generated Prime is not a factor,*/
/*the function
checks for the next prime number available*/
}
Is This Answer Correct ? | 9 Yes | 14 No |
Answer / mohammad nasim
include<stdio.h>
main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("\nThe prime factors are:\n");
while((n/2)!= 0 || (n/3)!=0)
{
if(n%2==0)
{
printf("\t2");
n=n/2;
}
else
{
if(n%3==0)
{
printf("\t3");
n = n/3;
}
else
{
printf("\t%d",n);
break;
}
}
}
}
Is This Answer Correct ? | 1 Yes | 6 No |
sir, i cannot find the way how to write aprogram by using array on queue
Can we declare function inside main?
How many levels of indirection in pointers can you have in a single declaration?
0 Answers Agilent, ZS Associates,
What language is windows 1.0 written?
What is a void pointer? When is a void pointer used?
What are the types of data types and explain?
size maximum allocated by calloc()
pgm to find middle element of linklist(in efficent manner)
what is the role you expect in software industry?
What is the benefit of using #define to declare a constant?
What is the use of function overloading in C?
a=5 a=a++/++a