C Program to Find All Factors of a Number

C Program to Find All Factors of a Number

A teacher wants every equal grouping size for twelve students. A grouping size works when division leaves no remainder.

A positive factor of n divides n exactly; enumerate candidates and keep those whose integer remainder is zero.

Before this exercise, review loops and stopping conditions and functions and parameters. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why all factors uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    List the factors of seven.

All Factors: follow the data

  1. Given data

    A teacher wants every equal grouping size for twelve students. A grouping size works when division leaves no remainder.

  2. Rule

    A positive factor of n divides n exactly; enumerate candidates and keep those whose integer remainder is zero.

  3. Observed result

    1 2 3 4 6 12

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

One and n are included for every positive n. A prime has only those two factors, while a composite also has factors between them.

This complete first approach scans every candidate in increasing order. A square-root approach can emit factor pairs faster, but must avoid duplicating a square root and preserve any promised output order.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int number = 12;
  for (int divisor = 1; divisor <= number; divisor++) {
    if (number % divisor == 0) printf("%d ", divisor);
  }
  putchar('\n');
  return 0;
}

Expected output

1 2 3 4 6 12 
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int number = 12;
  for (int divisor = 1; divisor <= number; divisor++) {
    if (number % divisor == 0) printf("%d ", divisor);
  }
  putchar('\n');
  return 0;
}

The candidates run from one through twelve.

Remainder-zero candidates are 1, 2, 3, 4, 6 and 12, printed in increasing order.

Follow the worked calculation

  1. Start with the sample

    The candidates run from one through twelve.

  2. Apply the rule

    Remainder-zero candidates are 1, 2, 3, 4, 6 and 12, printed in increasing order.

  3. Check the result

    1 2 3 4 6 12

    1 2 3 4 6 12
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int number = 12;
  for (int divisor = 1; divisor <= number; divisor++) {
    if (number % divisor == 0) printf("%d ", divisor);
  }
  putchar('\n');
  return 0;
}

List the factors of seven.

Worked answer

Only one and seven divide the prime input exactly, so the output has two factors.

C17 · complete program

#include <stdio.h>
int main(void) {
  int number = 7;
  for (int divisor = 1; divisor <= number; divisor++) {
    if (number % divisor == 0) printf("%d ", divisor);
  }
  putchar('\n');
  return 0;
}

Expected output

1 7 
Run in C compiler →

Recognize the important boundaries

Zero is not within this positive-natural-number contract; every nonzero integer divides zero, so a finite 1-through-n scan is not the same problem.

A loop through the maximum signed integer can overflow its counter after the last value. Bound user input or use a termination form that avoids that increment.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Does having a factor one prove that n is prime?
Choose one answer. You can try again.

Answer and explanation

No; every positive integer has factor one.

Correct. Primality depends on the absence of other proper positive factors.

Use the idea independently

A positive factor of n divides n exactly; enumerate candidates and keep those whose integer remainder is zero.

Continue with testing the problem’s boundary cases.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Check Sum of Two Prime Numbers →