Can someone help me with the codes in C language.this is the problem:
create a program that will display the factors of a given number.the input must read from a text file and consist of one or more lines,each line contains a number.
Sample input from a text file:

the factors of a given number should separated by a space per line.if the number has no factor,print out NO FACTOR.

Sample output:
2 3
2 5
2 4 5 10

THankz in advance...

Votes + Comments
He's new - let's cut him a little slack.
Asking us to do ur homework? Shame on you...
6 Years
Discussion Span
Last Post by v.r.t

Have you tried coding this at all?

The algorithm is pretty straight forward if you know a bit of math.

I think you should try doing it yourself first and post any problems you might have with the code, not expect someone from here to do your job for you ;)


Look up Sieve of Eratosthenes: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
That will allow you to build an array of prime numbers. Then you just need to see if your number is divisible by Prime(N) where N <= 1/2 X where X is your number. Another algorithm is if your number is even, divide by two until it isn't, then if the result ends in 5, divide by 5 until the last digit isn't 5, then factor that result. Example:

X = 100
X is even, so 2 is a factor.
X / 2 = 50
50 / 2 = 25, ends in 5 so 5 is a factor
25 / 5 = 5, 5 is prime, so no more factors
Results: factor(100) = {2, 5}

What about X = 101?
101 / 2 = 50 (drop fraction), but 50 is not prime
Is 50 prime? No, because it is divisible by 2 and 5
Is 49 prime? Yes. Is 101 divisible by 49? No
Is 47 prime? Yes. Is 101 ... ? No.
Is 43 prime? Yes. ... No.
Is 41 prime? Yes. ... No.
Is 39 prime? No. Divisible by 3 and 13.
Is 37 prime? Yes. ... No.
Is 33 prime? No. Divisible by 3 and 11.
Is 31 prime? Yes. Is 101 divisible by 31? No.
Is 29 prime? Yes. ... ? No.
Is 27 prime? No ... divisible by 3
23? Yes. Divisible? No.
21? No. 3 and 7
19? Yes. Divisible? No.
17? Yes. Divisible? No (6 x 17 = 102 - close but no banana)
13? Yes. Divisible? No.
11? Yes. Divisible? No.
7? Yes. Divisible? No.
5? Yes. Divisible? No.
3? Yes. Divisible? No.
Ok - done. 101 is prime, has no factors other than 1 and self.

There is a much more efficient binary search using a reasonably sized sieve of erathosthenes, but that is too advanced to cover here. I implemented that once and could determine if any number 15 digits or less was prime in 3-4 lookups or less. Very fast when combined with the heuristic rules I outlined above (even? Divide by 2 until remainder is not even. If remainder ends in 5, divide by 5 until not. Check remainder if prime using sieve. If not prime, find largest prime divisor <= 1/2 remainder, divide and check remainder again until remainder is prime... done).

Anyway, there is one important lesson here. ALWAYS work out your algorithm first, then write pseudo-code, then convert pseudo-code into the language of choice.

Edited by rubberman: n/a


Create a program that dispalys the factors of a given number
Input: 16
Ouput: 1 2 3 4 6 8 16


we are not here to write code for you. it is very necessary to show us your efforts in order to get assistance.

so , post your effors(your code) and then any assistance will be possible.

And , don't forget to create a new thread for a question. don't load too much in a single thread. right ?


Edited by Learner010

int main()
    int k,i;
    printf("ENTER A NUMBER :");
    return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.