Write a C program to enter any number from user and print all factors of the given number using for loop. How to find factors of any number in C programming. Logic to find all factors of a given number in C program.

**Example**

**Input**

Input number: 12

**Output**

Factors of 12: 1, 2, 3, 4, 6, 12

## Required knowledge

Basic C programming, If else, For loop, Basic Mathematics

### What is factor?

Factor of any number is a whole number which exactly divides any number into a whole number without leaving any remainder. For example: 2 is a factor of 6 because 2 divides 6 exactly leaving no remainder.

## Logic to find factors of any number

As per definition checking divisibility is the most important task we will be doing here.

Read more - Program to check divisibility of any number.

I have divided the logic to find factors of any given number to three simple steps. Let us walk by these steps.

- Input number from user. Store it in some variable say num.
- Run a loop from 1 to num, incrementing 1 in each iteration. The loop structure should look like for(i=1; i<=num; i++).
- Finally check the current loop variable is a factor of num or not. Perform modulo operation i.e. if(num % i == 0) then i is a factor of num.

## Program to find factors of any number

```
/**
* C program to print all factors of any number
*/
#include <stdio.h>
int main()
{
int i, num;
/* Input a number from user */
printf("Enter any number to find its factor: ");
scanf("%d", &num);
printf("All factors of %d are: \n", num);
/* Iterate from 1 to num */
for(i=1; i<=num; i++)
{
/*
* If num is exactly divisible by i
* Then i is a factor of num
*/
if(num % i == 0)
{
printf("%d, ",i);
}
}
return 0;
}
```

Output

Enter any number to find its factors: 100 All factors of 100 are: 1, 2, 4, 5, 10, 20, 25, 50, 100,

Happy coding 😉

<pre><code> ----Your Source Code---- </code></pre>