Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Factors of a number

anton19822anton19822 Member Posts: 12
Hey...

i have managed to create this short piece of code to enter a number and find its factors but i'm struggling to write the finishing program to find the number that is less than 1000 which has the most factors and print the factors without 1 and itself.

I know the number is 840 but not sure how to finish this off.

Can somebody help me please?

Thanx.....anton

#include
#include
long int x;
long int y=1;
main ()


{
printf("Enter value : ");
scanf("%d", &x);
printf("{");
for(y=1;y if(x%y==0)
printf("%d, ", y);
printf("%d}
", x);
return 0;
}

Comments

  • DonotaloDonotalo Member Posts: 715
    : Hey...
    :
    : i have managed to create this short piece of code to enter a number and find its factors but i'm struggling to write the finishing program to find the number that is less than 1000 which has the most factors and print the factors without 1 and itself.
    :
    : I know the number is 840 but not sure how to finish this off.
    :
    : Can somebody help me please?
    :
    : Thanx.....anton[code]
    :
    : #include
    : #include
    : long int x;
    : long int y=1;
    : main ()
    :
    :
    : {
    : printf("Enter value : ");
    : scanf("%d", &x);
    : printf("{");
    : for(y=1;y: if(x%y==0)
    : printf("%d, ", y);
    : printf("%d}
    ", x);
    : return 0;
    : }[/code]
    :
    [purple]
    where is the counter for counting factors? whenever y divides x, increment that counter variable by one. thus u will get the number of factors for x. this shud be done in an inner loop.

    in the outer loop, loop the variable x from 1 to 1000. keep another variable that will track the maximum number of factors found for the previous x. for example, when x = 10, then that variable shud contain 3, which is the number of factors of 9.

    keep another variable that contains the number whose maximum number of factors found so far. combining the above ideas u shud be able to code.
    [code]
    int max_fac = 0, x, factors;
    for (x = 1; x <= 1000; x++) {<br /> factors = [blue]1[/blue]; /* factors found so far for x */
    for ([blue]y = 2[/blue]; y <= x; y++)<br /> if (x%y == 0)
    /* some code here */
    if (factors > max_fac)
    /* some code here */
    }
    [/code]
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.