Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<conio.h>
- #include<STDIO.H>
- void main(void)
- {
- int number;
- int prime(int number);
- int primefactor(int number);
- printf("Enter the number whose prime factors are to be calculated:");
- scanf ("%d", &number);
- primefactor(number);
- getch();
- }
- //The following is the function that detects a Prime number.
- int prime(int num)
- {
- int i, ifprime;
- for (i=2; i<=num-1; i++)
- {
- if (num%i==0)
- {
- ifprime=0;
- }
- else
- ifprime=1;
- }
- return
- (ifprime);
- } //The following function prints the prime factors of a number.
- int primefactor(int num)
- {
- int factor,ifprime;
- for (factor=2; factor<=num;factor++)
- {
- prime(factor);
- //so that the factors are only prime and nothing else.
- if (ifprime)
- {
- if (num%factor==0) //diving by all the prime numbers less than the number itself.
- {
- printf("%d ", factor);
- num=num/factor;
- factor--;
- // continue;
- }
- // else
- // {
- // factor++;//this cannot be made a part of the for loop
- // }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement