# Program to Find Factors of a Number in C

Here we will write a C program to find factors of a number using while, do-while, or for loop. We will also write the same program using function, and recursion.

Factors are the numbers you multiply to get another number. For instance, factors of 12 are 3 and 4, because of 3×4 = 12. Some numbers have more than one factorization (more than one way of being factored). For instance, 20 can be factored as 1×20, 2×10 or 4×5. If a number has only factors 1 and itself that number is called a prime number.

The factor of a number never will be greater than half of the number. For example, half of 10 is 5, so factors of 10 will never be greater than 5.

Prerequisites for C Program to find Factors of a Number:-

## Program to find factors of a number in C using for loop

``````#include<stdio.h>
int main()
{
int num;

printf("Enter number: ");
scanf("%d",&num);

printf("Factors of %d are:\n", num);

for(int i=1; i<=num/2; i++)
{
if(num%i==0)
printf("%d\t", i);
}

return 0;
}``````

Output for different test-cases:-

Enter number: 12
Factors of 12 are:
1 2 3 4 6

Enter number: 28
Factors of 28 are:
1 2 4 7 14

## Program to find factor Using while loop

The while loop is pre-test loop, where firstly the condition is checked and if the condition is true then only the statements of the while loop execute.

``````#include<stdio.h>
int main()
{
int num, i=1;
printf("Enter number: ");
scanf("%d",&num);
printf("Factors of %d are:\n", num);
while (i<=num/2) {
if(num%i==0) printf("%d\t",i);
i++;
}
return 0;
}``````

## Using the do-while loop

The do-while loop is a post-test loop. In the do-while loop, the statements of the do-while loop are executed after that, the condition is evaluated, and if the condition is true then again the statements of the do-while loop are executed.

``````#include<stdio.h>
int main()
{
int num, i=1;

printf("Enter number: ");
scanf("%d",&num);

printf("Factors of %d are:\n", num);
do
{
if(num%i==0) printf("%d\t",i);
i++;
} while (i<=num/2);

return 0;
}``````

## C program to find the sum of factors of a number

Some times we need to find the factors and also the sum of the factors of the number. Below C program finds factors and the sum of the factors of a number. The logic for the below program is similar to the previous programs.

``````#include<stdio.h>
int main()
{
int num, sum=0;

printf("Enter number: ");
scanf("%d",&num);

printf("Factors of %d are:\n", num);
for(int i=1; i<=num/2; i++)
{
if(num%i==0)
{
sum += i;
printf("%d\t",i);
}
}

printf("\nSum of %d factors is: %d\n", num, sum);

return 0;
}``````

Output for different test-cases:-

Enter number: 12
Factors of 12 are:
` 1 2 3 4 6 `
Sum of 12 factors is: 16

Enter number: 20
Factors of 20 are:
` 1 2 4 5 10 `
Sum of 20 factors is: 22

## C Program to Find Factors of a Number using a function

A user-defined function also can be used to find factors of a number. A function is a block of code that performs a specific task. In C language a big program divided into several small subroutines/functions/procedures.

``````#include<stdio.h>
void findFactors(int n)
{
for(int i=1; i<=n/2; i++)
{
if(n%i==0)
printf("%d\t", i);
}
}

int main()
{
int number;

printf("Enter number: ");
scanf("%d",&number);

findFactors(number);

return 0;
}``````

## C Program to Find Factors of a Number using recursion

A function that contains a call to itself is called the recursive function. A technique of defining the recursive function is called recursion. The recursive function allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

``````#include<stdio.h>
void factors(int number, int i)
{
if ( i > number/2 ) return;
if(number%i == 0) printf("%d ", i);
factors(number, i+1);
}

int main()
{
int num;

printf("Enter number: ");
scanf("%d",&num);

printf("Factors of %d are:\n", num);
factors(num, 1);

return 0;
}``````

Output for different test-cases:-

Enter number: 12
Factors of 12 are:
` 1 2 3 4 6 `

Enter number: 20
Factors of 20 are:
` 1 2 4 5 10`

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!