✓ Link copied to clipboard
C 📅 July 7, 2026 ⏱ 2 min read

C Program to Display Factors of a Number

C Program to Display Factors of a Number

When you're coding in C, figuring out the factors of a number means checking which integers divide it evenly. If you've done any math with divisors, this should ring a bell. Take the number 12, for instance. Its factors are 1, 2, 3, 4, 6, and of course, 12. Let’s dive into how to do this with a straightforward C program.

Prerequisites for Displaying Factors Program in C

Before writing the code, ensure you have these ready:

  • Understanding of int data type to store integers.
  • Familiarity with the for loop, which we'll use to iterate through possible factors.
  • Knowledge of the modulus operator %% to check divisibility.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Display Factors of a Number

Here's the C code to find and display all factors of a given number.

factors.cC▶ Run
#include <stdio.h>

int main() {
    int n, i;
    // Ask the user for input
    // Display factors of the number
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factors of %d are: ", n);
    for (i = 1; i <= n; i++) {
        if (n %% i == 0) {
            printf("%d ", i);
        }
    }
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Display Factors Program

When you run the program and input a number, it lists all its factors.

OutputTerminal
Enter a number: 12
Factors of 12 are: 1 2 3 4 6 12 
Output of C Program to Display Factors of a Number
Output of C Program to Display Factors of a Number

Algorithm to Display Factors in C

Displaying factors involves dividing the number by all smaller or equal integers. Here is the exact logic the program uses, step by step.

  1. Read the integer n from the user.
  2. Initialize i to 1.
  3. Check if i is less than or equal to n.
  4. If n %% i == 0, print i as a factor.
  5. Increment i by 1 and repeat steps 3-5.
  6. End when all possible factors are checked.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C[i = 1]
  C --> D{"i <= n?"}
  D -->|Yes| E{"n %% i == 0?"}
  E -->|Yes| F[/Print i/]
  E -->|No| G[i++]
  F --> G
  G --> D
  D -->|No| H([End])
Flowchart: the algorithm step by step

How the C Program to Display Factors Works

Let's break down the code to understand each part:

  • #include <stdio.h> includes the standard input-output library, essential for using printf and scanf.
  • The main function begins the program execution.
  • Variables n and i are declared as int to store the number and the iterator.
  • printf("Enter a number: ") prompts the user for input.
  • scanf("%d", &n) reads the user input and stores it in n.
  • The for loop runs from 1 to n. For each i, if n %% i is 0, i is a factor and printed.
  • return 0 indicates the program executed successfully.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the purpose of using the modulus operator in this program?

🎉Correct! The modulus operator checks if there is no remainder, indicating a factor.
🤔Not quite. Think about how we identify factors mathematically.

Time and Space Complexity of the C Program to Display Factors

This program's efficiency can be measured by its time and space complexity.

Time Complexity: O(n). Here, n is the number you input. The for loop iterates through every integer from 1 to n, as seen in for (i = 1; i <= n; i++). If you input 100, the loop runs 100 times, checking each integer to see if it's a factor.

Space Complexity: O(1). The program uses a fixed amount of space. It only requires n for the input and i for iteration, so regardless of the input size, it doesn't need extra memory.

⚠️

Common mistake: Forgetting to check for factors beyond half of the number can miss factors, but this code correctly checks up to the number itself.

Key takeaway: Checking divisibility with the modulus operator is key for finding factors effectively.

FAQs About Displaying Factors in C

Here are some common questions about this program.

A factor is just a number that divides another number fully without leaving a remainder.
The modulus operator checks if dividing two numbers leaves a remainder. If it’s zero, then the divisor's a factor.
Yeah, but it’ll take longer with bigger numbers since it’s O(n) complexity.
The loop needs to check each number up to n to find all possible factors.
For this program, it uses a constant amount of space no matter how big the input is.

If you found this program helpful, check out these related programs:

Conclusion

To really get the hang of this code, try different numbers and see how fast you can find the factors. It’s a great way to practice loops and conditions in C. If you're interested, check out prime factorization next — it’s a cool extension of this idea.

Try out more C programs and sharpen your skills with our C quizzes and online compiler.