Display all Factors of a Number Program in C Language

To print all the factors of a number n in C programming, iterate from 1 to n in a loop, and during each iteration check if this number divides n with zero reminder. All those numbers that leave zero reminder are the factors of the given number.

C Program

In the following program, we read a number to n from user via console input, and print all the factors of this number. We use C For Loop to iterate from 1 to give number.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n;
    printf("Enter a number : ");
    scanf("%d", &n);
    
    for (int i = 1; i <= n; ++i) {
        if (n % i == 0) {
            printf("%d  ", i);
        }
    }
    printf("\n");
}

Output

Enter a number : 20
1  2  4  5  10  20  
Program ended with exit code: 0

Output

Enter a number : 8
1  2  4  8  
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned how to display all the factors of a given number in C programming, with examples.