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:

**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. You will check whether numbers from 1 to num are divisible by num or not. If divisible then it is a factor otherwise not. Read more about checking divisibility.

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

- Read the 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; /* Read 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; }

Enter any number to find its factors: 100

All factors of 100 are: 1, 2, 4, 5, 10, 20, 25, 50, 100,

Happy coding ;)

### Recommended posts

- Loop programming exercises index.
- C program to find all Prime factors of a number.
- C program to find factorial of a number.
- C program to find power of any number using for loop.
- C program to check Prime numbers.
- C program to check Armstrong numbers.
- C program to check Strong numbers.
- C program to check Perfect numbers.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)