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 the differnce between programing langauge and tool? is sas is a programing langauge r tool?
Write a program in c using only loops to print * * * * * *******
Read the following data in two different files File A: aaaaaaaadddddddd bbbbbbbbeeeeeeee ccccccccffffffff File B: 11111111 22222222 33333333 By using the above files print the following output or write it in the Other file as follows aaaaaaaa11111111dddddddd bbbbbbbb22222222eeeeeeee cccccccc33333333ffffffffffff
Why array is used in c?
How will you allocate memory to double a pointer?
Is main a keyword in c?
Write a code to generate a series where the next element is the sum of last k terms.
Is there a way to jump out of a function or functions?
how to swap 2 numbers in a single statement?
What is floating point exception error? And what are different types of errors occur during compile time and run time? why they occur?
can you explain in brief what is "r+" mode in a file... i know that it si used to read and modify rhe existing content.... but explalanation about the file pointer in "r+" mode i wann to know???????????
What is a wrapper function in c?