C Programming Code Examples C > For Loops and While Loops Code Examples program to find all factors of a number program to find all factors of a number Write a C program to input a number from user and find all factors of the given number using for loop. How to find factors of a number in C program. What is factor of a number? Factor of any number is a whole number which exactly divides the 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 all factors of a number Input number from user. Store it in some variable say j. Run a loop from 1 to j, increment 1 in each iteration. The loop structure should look like for(x=1; x<=j; x++). For each iteration inside loop check current counter loop variable x is a factor of j or not. To check factor we check divisibility of number by performing modulo division i.e. if(j % x == 0) then x is a factor of j. If x is a factor of j then print the value of x. #include <stdio.h> int main() { int x, j; /* Input number from user */ printf("Enter any number to find its factor: "); scanf("%d", &j); printf("All factors of %d are: \n", j); /* Iterate from 1 to j */ for(x=1; x<=j; x++) { /* * If j is exactly divisible by x * Then x is a factor of j */ if(j % x == 0) { printf("%d, ",x); } } return 0; }