C Program to Find Factors of a Number


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

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

Related Read:
Basic Arithmetic Operations In C
while loop in C programming

Logic To Find Factors of a Number

We ask the user to enter a integer number. Next we iterate through the while loop until the 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

#include<stdio.h>

int main()
{
    int num, count = 1;

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

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

    while(count <= num)
    {
        if(num % count == 0)
        {
            printf("%d\n", count);
        }
        count++;
    }

    return 0;
}

Output 1:
Enter a number
50
Factors of 50 are:
1
2
5
10
25
50

Output 2:
Enter a number
200
Factors of 200 are:
1
2
4
5
8
10
20
25
40
50
100
200

Output 3:
Enter a number
400
Factors of 400 are:
1
2
4
5
8
10
16
20
25
40
50
80
100
200
400

Output 4:
Enter a number
500
Factors of 500 are:
1
2
4
5
10
20
25
50
100
125
250
500

C Program to Find Factors of a Number using While Loop


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

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


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

Leave a Reply

Your email address will not be published. Required fields are marked *