C Program To Display Factors of a Number

In this post, we will learn how to display factors of a number using C Programming language. 

C Program To Display Factors of a Number

Factors of a number are defined as numbers that divide the original number evenly or exactly without giving any remainder.
For example: 6 is a factor of 72 because dividing 72 by 6 gives no remainder.

Similarly, in this program we will be using the same logic to find and display the factors of a number.

So, without further ado, let’s begin the tutorial.

C Program To Display Factors of a Number

// C Program To Display Factors of a Number
#include <stdio.h>
int main(){
    int num, i;
    
    // Asking for Input 
    printf("Enter a positive number: ");
    scanf("%d", &num);
    
    printf("Factors of %d are:\n", num);
    
    // logic
    for (i = 1; i <= num; ++i){
        if (num % i == 0){
            printf("%d\n", i);
        }
    }
    return 0;
}

Output

Enter a positive number: 72
Factors of 72 are:
1
2
3
4
6
8
9
12
18
24
36
72

How Does This Program Work ?

    int num, i;

In this program, we have declared two int data type variables named num and i.

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

Then, the user is asked to enter a positive number. The value of the number will get stored in the num variable.

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

The for loop is iterated until i is false. Then, for each iteration it checks whether num is exactly divisible by i.

If the condition is true then that value of i is a factor of the number. 

After that the value of i is incremented by 1 to check for the next value and this process will continue until i <= num.

Conclusion

I hope after going through this post, you understand how to display factors of a number using C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

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