C Program to Find Factors of a Number using For Loop


Write a C program to display Factors of user entered number, using for loop. All the numbers which perfectly divide a given number are called as Factors of that number.

For Example, if user enters integer number 50. All the numbers which perfectly divide the number 50 are called Factors of number 50.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming
C Program to Find Factors of a Number

Video Tutorial: C Program to Find Factors of a Number using For Loop


[youtube https://www.youtube.com/watch?v=sKcL5I3kOIo]

YouTube Link: https://www.youtube.com/watch?v=sKcL5I3kOIo [Watch the Video In Full Screen.]


Logic To Find Factors of a Number using For Loop

We ask the user to enter a integer number. Next we iterate through the for loop until count is less than or equal to the user entered number. Example, if user entered number is 50, then we iterate through the loop for 50 times. Each time we check if the user entered number is perfectly divisible by the value of count. Initial value of count is 1 and after each iteration of the loop count value increments by 1. Whenever a number perfectly divides the user entered number, we display it as factor of the user entered number.

Source Code: C Program to Find Factors of a Number using For Loop

#include<stdio.h>

int main()
{
    int num, count;

    printf("Enter a number to find factors\n");
    scanf("%d", &num);

    printf("Factors of %d are\n", num);
    for(count = 1; count <= num; count++)
    {
        if(num % count == 0)
            printf("%d\n", count);
    }

    return 0;
}

Output 1:
Enter a number to find factors
60
Factors of 60 are
1
2
3
4
5
6
10
12
15
20
30
60

Output 2:
Enter a number to find factors
25
Factors of 25 are
1
5
25

Output 3:
Enter a number to find factors
40
Factors of 40 are
1
2
4
5
8
10
20
40

Output 4:
Enter a number to find factors
75
Factors of 75 are
1
3
5
15
25
75

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert