1.11M Members

create a program that will display the factors of a given number

 
-4
 

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:
6
5
10
20

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
NO FACTOR
2 5
2 4 5 10

THankz in advance...

 
0
 

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 ;)

 
0
 

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.

 
0
 

Did you [thread=78060]Read This Before Posting[/thread]?

 
0
 

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

 
0
 

@john
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 ?

thanks.

 
0
 
#include<stdio.h>
int main()
{
    int k,i;
    printf("ENTER A NUMBER :");
    scanf("%d",&k);
    for(i=1;i<=k;i++){
        if(k%i==0)
            printf("%d\n",i);
        }
    return 0;
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article