Write a program to generate prime factors of a given integer?
Answer Posted / 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 |
Post New Answer View All Answers
How do you sort filenames in a directory?
Process by which one bit pattern in to another by bit wise operation is?
What are dangling pointers in c?
What is the right way to use errno?
What is typedef?
What are the uses of null pointers?
How can I run c program?
What is the difference between scanf and fscanf?
What are the advantages and disadvantages of c language?
Why do we use header files in c?
A character flag or control mechanism that delineates one data item from another a) variable b) constant c) delimiter d) call by reference
How can I implement a delay, or time a users response, with sub-second resolution?
i want to know the procedure of qualcomm for getting a job through offcampus
Hi how many types of software editions are there and their difference (like home editions, enterprise, standard etc) can u please help me
What are the two types of structure?