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 C. 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 program allows the user to enter any integer value. Using this value, this C program 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**

Within this program to find factors of a number in C, We declared two integer variables i and number.

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, Number = 4 – It means (2 <= 4) is True

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

**Third Iteration**

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

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

**Fourth Iteration**

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

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

i++ means i will become 5 – It means (5 <= 4) is False. 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 find Factors of a number using While Loop.

We just replaced the For loop in the above Program to Find Factors of a Number using for loop 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 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**

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

This program to Find Factors of a Numbering C pass the user entered value to the Function. Within this User defined function, we use For loop to 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**

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

This C program 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**

In this Program to Find Factors of a Number, 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 the address.