0
0
CProgramBeginner · 2 min read

C Program to Find Factors of a Number

To find factors of a number in C, use a for loop from 1 to the number and print all values that divide the number evenly using if (num % i == 0).
📋

Examples

Input6
OutputFactors of 6 are: 1 2 3 6
Input13
OutputFactors of 13 are: 1 13
Input1
OutputFactors of 1 are: 1
🧠

How to Think About It

To find factors of a number, think about all numbers from 1 up to that number. For each, check if dividing the number by it leaves no remainder. If yes, that number is a factor.
📐

Algorithm

1
Get the input number from the user.
2
Start a loop from 1 to the input number.
3
For each number in the loop, check if it divides the input number evenly (remainder zero).
4
If yes, print that number as a factor.
5
End the loop after reaching the input number.
💻

Code

c
#include <stdio.h>

int main() {
    int num, i;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Factors of %d are: ", num);
    for(i = 1; i <= num; i++) {
        if(num % i == 0) {
            printf("%d ", i);
        }
    }
    return 0;
}
Output
Enter a number: 6 Factors of 6 are: 1 2 3 6
🔍

Dry Run

Let's trace the input 6 through the code to find its factors.

1

Input number

num = 6

2

Start loop from 1 to 6

i = 1 to 6

3

Check if 6 % i == 0

For i=1: 6 % 1 = 0 (print 1) For i=2: 6 % 2 = 0 (print 2) For i=3: 6 % 3 = 0 (print 3) For i=4: 6 % 4 = 2 (skip) For i=5: 6 % 5 = 1 (skip) For i=6: 6 % 6 = 0 (print 6)

inum % iPrint factor?
10Yes (1)
20Yes (2)
30Yes (3)
42No
51No
60Yes (6)
💡

Why This Works

Step 1: Loop through numbers

The program checks every number from 1 to the input number to find possible factors.

Step 2: Check divisibility

Using num % i == 0 tests if the number divides evenly with no remainder.

Step 3: Print factors

If the condition is true, the current number is a factor and gets printed.

🔄

Alternative Approaches

Check factors up to half the number
c
#include <stdio.h>

int main() {
    int num, i;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Factors of %d are: 1 ", num);
    for(i = 2; i <= num / 2; i++) {
        if(num % i == 0) {
            printf("%d ", i);
        }
    }
    if(num != 1) printf("%d", num);
    return 0;
}
This reduces the loop range to half the number since no factor (except the number itself) can be greater than half, improving efficiency.
Check factors using square root optimization
c
#include <stdio.h>
#include <math.h>

int main() {
    int num, i;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Factors of %d are: ", num);
    int limit = (int)sqrt(num);
    for(i = 1; i <= limit; i++) {
        if(num % i == 0) {
            printf("%d ", i);
            if(i != num / i) printf("%d ", num / i);
        }
    }
    return 0;
}
This method checks up to the square root and prints both factors at once, making it faster for large numbers but factors may print unordered.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops from 1 to the input number, so it runs in linear time O(n). Each iteration does a simple modulus check.

Space Complexity

The program uses a fixed amount of memory for variables and prints results directly, so space complexity is O(1).

Which Approach is Fastest?

Using the square root optimization reduces time to about O(√n), which is faster for large numbers compared to the simple O(n) loop.

ApproachTimeSpaceBest For
Simple loop 1 to nO(n)O(1)Small to medium numbers, easy to understand
Loop 1 to n/2O(n/2) ~ O(n)O(1)Slightly faster, still simple
Square root optimizationO(√n)O(1)Large numbers, better performance
💡
Always check divisibility using the modulus operator % to find factors.
⚠️
Beginners often forget to include the number itself as a factor or start the loop from 0 causing division errors.