Write a program in c to find the prime factors of a given number

Write a program in c to find the prime factors of a given number

Done.

Betlista

12 Likes

1 - Find prime numbers using Sieve of Eratosthenes (you can just find primes numbers <= sqrt(n))

2 - Here is an algorithm to find prime factors

// suppose primes is an array of prime numbers
// result is an array which will contains prime factors
// i is the current prime , i = 0
while primes[i] < n
     if n % primes[i] is 0
         append primes[i] to result
     while n % primes[i] is 0
         n /= primes[i]
     // increment i
     i += 1
if n := 1
     append n to result
// finnaly the result array will contains prime factors of the number n

3 - Good Luck and happy coding … :smiley:

#inlcude<stdio.h>
int main()
{
int n,i=2;
scanf("%d",&n);
while(n!=1)
{
if(n%i==0)
{
printf("%d",i);
n=n/i;
}
else
i++;
}
}
hope u understand.All the best.

//Program to accept a number and print its prime factors
#include<stdio.h>
#include<conio.h>
int main()
{
int n,k=2,j;
clrscr();
printf(“Enter the number\n”);
scanf("%d",&n);
while(k<n)
{
if(n%k==0)
{
n=n/k;
printf("%4d",k);
if(n%k==0)
printf("%4d",k);
}
k++;
}
getch();
return 0;
}

I couldn’t stop laughing. :stuck_out_tongue:

Just the right answer for a question like this. Good work @betlista