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 |
what is pointer ?
10 Answers Kernex Micro Systems,
What is the process to generate random numbers in c programming language?
Write a program to enter the name and age. If age>28 then find salary categories. if age<28 then find that you are gaduate or not.
what is the output of the following program? #include<stdio.h> void main() { float x=1.1; while(x==1.1) { printf("\n%f",x); x=x-0.1; } }
will u please send me the placement papers to my mail???????????????????
What is register variable in c language?
Consider the following C program. #include <stdio.h> int main() { int i; for (i=0;i<3;++i) { fork();fork(); } } How many processes are created when running this program (including the initial one)? Explain
2. Counting in Lojban, an artificial language developed over the last fourty years, is easier than in most languages The numbers from zero to nine are: 0 no 1 pa 2 re 3 ci 4 vo 5 mk 6 xa 7 ze 8 bi 9 so Larger numbers are created by gluing the digit togather. For Examle 123 is pareci Write a program that reads in a lojban string(representing a no less than or equal to 1,000,000) and output it in numbers.
Why does notstrcat(string, "!");Work?
What are local static variables?
write a program for 7*8 = 56 ? without using * multiply operator ? output = 56
An entire structure variable can be assigned to another structure variable if __________
3 Answers Sasken, TCS, Tech Mahindra, Wipro,