i am trying to make a program that displays to the screen the prime factors of a given number. i have it all the way to the prime number checking i dont quite understand how to extract the prime numbers any help would be much appreciated

Recommended Answers

All 5 Replies

i am trying to make a program that displays to the screen the prime factors of a given number. i have it all the way to the prime number checking i dont quite understand how to extract the prime numbers any help would be much appreciated

I'm not sure I know what you mean by having it "all the way to prime number checking". I'll guess that you mean that you know how to check whether the given number is a prime or not. If you find that it's a prime, then you're finished. Suppose the given number is N and you've found that it's not a prime. You've found this out, I guess, by finding a number d such that d is a divisor of N. (i.e., N % d == 0). The apply your method to N/d.

A prime no is the one which either divides by itself or by 1 .....So u create a loop from 1 to that number and keep on counting how many times it exactly gets divides ...if it comes to 2 then u can say it is prime number....the code can look like this.....

scanf("%d",&n);
for(i=1;i<=n;i++)
{ if((i % n) == 0)
count++;
}

if(count==2)
printf("It is prime no");

Hey i think i misread the question......U want Prime factors of a no or U want to display prime number

i am not supposed to use any loops. it was supposed to go like i if i gave it 10 it would give me 2*5 as an output something like that

Ha ha! There are so many ways to do it. The easiest process is to try divide the number with every number less than (or half of it) and see if it has the remainder 0. If it does then check if the number u r dividing with is a prime number or not. This is the easiest process although is not efficient for large numbers. For other ways about doing this prime factorization go to mathworld

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.