---
title: Display factors of a number
url: https://docs.vultr.com/clang/examples/display-factors-of-a-number
publish_date: 2024-12-04T18:40:00.098478Z
last_updated: 2024-12-04T18:40:00.098478Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1254/82564b42-7118-45a5-b7d9-1ae49f06fb89.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# C Program to Display Factors of a Number

## Introduction
Factors of a number in programming can be utilized to perform various tasks such as simplifying fractions, algorithm optimization, and in educational tools that demonstrate basic concepts of mathematics. A factor of a number refers to any integer which divides the given number without leaving a remainder.

In this article, you will learn how to create a C program to find factors of a number. This will include setting up the basic structure of a C program, implementing the logic to find and display factors, and running example scenarios to see the program in action.

## Implementing the Factor Display Function

### Setup the Basic C Program Structure
1. Start by including the necessary libraries and declare the main function:
    ```c
    #include <stdio.h>

    int main() {
        // Program code will go here
        return 0;
    }
    ```

### Create a Function to Find and Print Factors in C
1. Define a function that takes an integer as an argument and prints all its factors:
   
    ```c
    void printFactors(int num) {
        printf("Factors of %d are: ", num);
        for (int i = 1; i <= num; ++i) {
            if (num % i == 0) {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    ```

    The function `printFactors` iterates from `1` to the number itself, checking if the number is divisible by each integer `i`. If the number is divisible (i.e., `num % i == 0`), `i` is printed as a factor.

### Add Function Calls in the Main Program
1. Modify the `main()` function to prompt the user for a number and display its factors:

    ```c
    int main() {
        int number;
        printf("Enter a number: ");
        scanf("%d", &number);
        
        printFactors(number);
        
        return 0;
    }
    ```

    This code obtains an integer from the user, then calls the `printFactors()` function to display the factors of the entered number.

## Running Example Scenarios

### Example 1: Finding Factors of a Small Number
1. Compile and run the program, then input `6`:

    ```console
    Enter a number: 6
    Factors of 6 are: 1 2 3 6
    ```

    In this example, the factors of `6` are `1`, `2`, `3`, and `6` which the program correctly identifies and displays.

### Example 2: Finding Factors of a Larger Number
1. Try a larger number, such as `36`:

    ```console
    Enter a number: 36
    Factors of 36 are: 1 2 3 4 6 9 12 18 36
    ```

    The program identifies all factors of `36` from `1` to `36`, including divisor pairs that multiply to `36`.

## Conclusion
The C program for displaying factors of a number efficiently determines and prints all whole number divisors of any given number. This small project enhances your understanding of loops, conditionals, and basic input/output in C. Implementing such programs not only deepens your understanding of fundamental programming concepts but also gives practical insight into mathematical applications in programming, offering a methodical approach to problem-solving and debugging in software development.