The numbers that are completely divisible by the given number (it means the remainder should be 0) are called as factors of a given number. In this article, we will show you, How to write a C Program to find Factors of a Number using FOR LOOP, WHILE LOOP, Pointers and FUNCTIONS

## C Program to Find Factors of a Number Using For Loop

This C program allows the user to enter any integer value. Using this value, this program will find Factors of a number using For Loop .

/* C Program to Find factors of a number */ #include <stdio.h> int main() { int i, Number; printf("\n Please Enter any number to Find Factors \n"); scanf("%d", &Number); printf("\n Factors of the Given Number are:\n"); for (i = 1; i <= Number; i++) { if(Number%i == 0) { printf(" %d ", i); } } return 0; }

**OUTPUT**

**ANALYSIS**

We declared two integer variables i and number. Below printf statement will ask the user to enter his/her own integer value to Find factors of that number

printf("\n Please Enter any number to Find Factors \n");

Below scaf statement will assign the user entered value to the Number variable

scanf("%d", &Number);

**For Loop**

We initialized the integer i value to 1 and also (i <= Number) condition will help the loop to terminate when the condition fails.

for (i = 1; i <= Number; i++) { if(Number%i == 0) { printf(" %d ", i); } }

Within the for loop, there is If statement to check, whether the Number divisible by i is exactly equal to 0 or not. If the condition is True then it will print that i value else it will skip that particular value and check the condition for the next value.

User entered integer in the above example is 4

**First Iteration**

i = 1 and Number = 4 – It means (i <= Number) is True

Now, Check the if condition – if (Number%i == 0)

Number%i == 0 => (4%1 ==0) – This condition is TRUE. So, 1 is printed

i++ means i will become 2

**Second Iteration**

i = 2 and Number = 4 – It means (i <= Number) is True

Now, Check for the if condition – if(Number%i == 0)

if(4%2 ==0) – This condition is TRUE So, 2 is printed

i++ means i will become 3

**Third Iteration**

i = 3 and Number = 4 – It means (i <= Number) is True

Now, Check for the if condition – if(Number%i == 0)

if(3 % 2 ==0) – This condition is FLASE So, 3 Will be Skipped

i++ means i will become 4

**Fourth Iteration**

i = 4 and Number = 4 – It means (i <= Number) is True

Now, Check for the if condition – if (Number%i == 0)

if(4 % 4 ==0) – This condition is TRUE So, 4 is printed

i++ means i will become 5 – It means (i <= Number) is Flase. So, For loop will be Terminated.

Factors of 4 = 1, 2, 4

## C Program to Find Factors of a Number Using While Loop

This program allows the user to enter any integer value. Using this value, this program will find Factors of a number using While Loop.

/* C Program to Find Factors of a Number using While Loop */ #include <stdio.h> int main() { int Number, i = 1; printf("\n Please Enter number to Find Factors\n"); scanf("%d", &Number); printf("\n The Factors of a Number are:\n"); while (i <= Number) { if(Number%i == 0) { printf("%d ", i); } i++; } return 0; }

**OUTPUT**

**ANALYSIS**

We just replaced the For loop in the above example with the While loop. If you don’t understand the While Loop then please refer While Loop article here: WHILE LOOP

## C Program to Find Factors of a Number Using Functions

This program allows the user to enter any integer value. User entered value will be passed to the Function we created. Within this User defined function, this program will find Factors of a number using For Loop.

/* C Program to Find Factors of a Number using Functions */ #include <stdio.h> void Find_Factors(int); int main() { int Number; printf("\n Please Enter number to Find Factors\n"); scanf("%d", &Number); printf("\n Factors of a Number are:\n"); Find_Factors(Number); return 0; } void Find_Factors(int Number) { int i; for (i = 1; i <= Number; i++) { if(Number%i == 0) { printf("%d ", i); } } }

**OUTPUT**

**ANALYSIS**

Since the function Find_Factors() doesn’t return any value we declared the function definition with void keyword.

When the compiler reaches to Find_Factors(Number) line in main() program then the compiler will immediately jump to below function:

void Find_Factors(int Number)

We already explained LOGIC in the above example. If you did not understand the functions, Please refer Functions in C article for better understanding

## C Program to Find Factorial of a Number Using Pointers

This program allows the user to enter any integer value. Using this value, this program will find Factors of a number using pointers. Please Refer Pointers in C article before this example. It will help you to understand the Pointers and Pointer variable Concept.

/* C Program to Find Factors of a Number using Pointers */ #include <stdio.h> void Find_Factors(int *); int main() { int Number, *P; printf("\n Please Enter number to Find Factors\n"); scanf("%d", &Number); P = &Number; printf("\n Factors of a Number are:\n"); Find_Factors(P); return 0; } void Find_Factors(int *Number) { int i; for (i = 1; i <= *Number; i++) { if(*Number % i == 0) { printf("%d ", i); } } }

**OUTPUT**

**ANALYSIS**

We assigned the address of the Number variable to the address of the pointer variable.

P = &Number;

Here, P is the address of the pointer variable that we already declared (*P) and we all know &Number is the address of the Number.

Within the For Loop, We checked i value against the pointer variable *Number because *Number means the Value inside the pointer variable instead of address.

Thank you for Visiting Our Blog