1. ## Factorising problem

I am writing a simple prgram to show that factors of an integer input

I'm not sure if i'm going about writing this the right way, I get an output, but I need to somehow restrict the display to only show the integer results instead of all the results i have written the following:

Code:
```#include <stdio.h>

int main (void) {

int input;
int a;
int k;

printf("Enter number to be factorised:\n");
scanf("%d", &input);

printf("the factors of %d are\n", input);

for (k=1; k<=input; k++) {

a = input/k;

printf("%d\n", a);

}

return 0;
}```
But the output of this is not what i want, say if i enter 7, t gives me

Code:
```the factors of 7 are
7
3
2
1
1
1
1```
but what i want it to show is, say if i enter 1001

Code:
```the factors of 1001 are
1001
143
91
77
13
11
7
1```
any suggestions? 2. If you want to check that k is a factor of input, check that it divides into input with no remainder. 3. use something like
Code:
`if (input%k==0)`
do not use
Code:
`a = input/k;`
Also, try thinking of ways to optimize your algorithm 4. Hmm.... my C coding probably blows, but here is what went through my snoodle.

Code:
```#include <stdio.h>
#include <stdlib.h>

#define BUFF 30

int main (void) {

int input, value, upper, k;
static int array[BUFF];
int i = 0;

printf("Enter number to be factorised:\n");

/*doesn't handle alphanumberic chars at the end of number*/
if(scanf("%d", &input) != 1)
exit(1);

printf("the factors of %d are\n", input);

for (k=1 ; k<=input; k++) {
value = input %k;
if (value == 0) {
array[i] = k;
i++;
}
}

upper = i-1;

for(;((upper>=0) && upper<BUFF);upper--) {
printf("%d\n", array[upper]);
}

return 0;
}```
\$gcc -g -Wall factor.c -o factor
\$./factor
Enter number to be factorised:
7
the factors of 7 are
7
1
\$./factor
Enter number to be factorised:
1001
the factors of 1001 are
1001
143
91
77
13
11
7
1
\$./factor
Enter number to be factorised:
8
the factors of 8 are
8
4
2
1
\$./factor
Enter number to be factorised:
b
\$./factor
Enter number to be factorised:
b100
\$./factor
Enter number to be factorised:
\$

Ohh..... my aching hips. Code:
```#include <stdio.h>

int main(void)
{
long factor, input;
puts("Enter a number:");
scanf("%ld", &input);

puts("The factors are:");
printf("%ld\n", input);

for (factor = input/2; factor > 0; --factor)
if (input%factor==0)
printf("%ld\n", factor);

return 0;
}```
The following is a single run of the program:
Enter a number:
1001
The factors are:
1001
143
91
77
13
11
7
1 6. Hmmm..... I wasn't aware you could do that. You win. Pull my finger. 7. You might also see if you can improve further upon that. Everything you find that input % factor == 0, you can get two factors. factor is the obvious one, but input / factor is also one.
Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
long factor, endfact, input, factor2;
puts("Enter a number:");
scanf("%ld", &input);

puts("The factors are:");
printf("1\n%ld\n", input); // 1, input are always factors.

for(factor = 2, endfact = sqrt(input); factor <= endfact; ++factor)
{
if(input % factor == 0)
{
factor2 = input / factor;
printf("%ld\n", factor);
if(factor2 != factor)
printf("%ld\n", factor2);
}
}

return 0;
}```
Uses less than sqrt(input) iterations, but the factors are out of order. (But it should be easy to order, if you know how many there are: they go smallest, biggest, 2nd-smallest, 2nd-biggest, etc, working towards a middle.) 8. Code:
```#include <stdio.h>

int main (void)
{
int n;
int i;

printf("Number: ");
scanf("%d", &n);

if (n <= 1) {
return 1;
}
for (i = 2; n != 1; i++) {
while (n % i == 0) {
printf("%d ", i);
n /= i;
}
}

return 0;
}```
27 = 3 3 3
50 = 2 5 5
5642 = 2 7 13 31 9. So I was bored... but this time the factors are in order:

Code:
```#include <stdio.h>
void factorize(const int n, int last)
{
for(++last; n % last; ++last)
{
if(last * last == n)
{
printf("%d\n", n);
return;
}
if(last * last > n)
{
return;
}
}
if(last * last == n)
{
printf("%d\n", last);
return;
}
printf("%d\n", last);
factorize(n, last);
printf("%d\n", n / last);
}
int main()
{
int n;
printf("Number: ");
scanf("%d", &n);
factorize(n, 1);
return 0;
}``` Popular pages Recent additions 