C program to find all factors of a number

Write a C program to enter any number from user and print all factors of given number using for loop. How to find factors of any number in C programming. C program to display all factors of any number using loop.

Input number: 12
Output factors of 12: 1, 2, 3, 4, 6, 12

Required knowledge

Basic C programming, If else, For loop

Factor of a number

A factor of any number is a whole number which exactly divides any number into a whole number without leaving any remainder. For example: 2 is a factor of 6 because 2 divides 6 exactly leaving no remainder.

Logic to find factors of any number

Finding factors of any number will be easy if you know how to check whether a number is exactly divisible by any number or not. Now what we need to do here is just iterate from 1 to n (where n is the number whose factor is to be found) and print all those numbers that are exactly divides n.

Program to find factors of any number

 * C program to print all factors of any number

#include <stdio.h>

int main()
    int i, num;

    /* Reads number whose factors is to be found */
    printf("Enter any number to find its factor: ");
    scanf("%d", &num);

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

    /* Iterates from 1 to n */
    for(i=1; i<=num; i++)
         * If num is exactly divisible by i
         * Then i is a factor of num

    return 0;

Enter any number to find its factors: 100
All factors of 100 are:

Happy coding ;)

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment