Example HTML page

C Program to print the Factors of a given number

The following program displays the factors of a given number. For example, if the user enters 7, it displays 1 and 7. 

since 1*7 =7, and 7*1=7.

We can also extend this program to check this number is a prime number or not by counting the number of factors. for example if the total number of factors is 2, then the given number is a prime number, otherwise, it is not a prime number. 

#include <stdio.h>
int main()
{
    int number, i;

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

    printf("Factors of %d are: ", number);
    for(i=1; i <= number; ++i)
    {
        if (number%i == 0)
        {
            printf("%d ",i);
        }
    }

    return 0;
}
Factors of a given positive integer

Output:

Command Prompt
Enter any positive integer: 24
Factors of 24 are:
1
2
3
4
6
8
12
24
——————————–
Process exited after 230.3 seconds with return value 0
Press any key to continue . . .
Example HTML page

Leave a Reply

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

Pin It on Pinterest