C Program to Display Factors of a Number

You will learn how to determine all the factors of an integer entered by the user in this example.

To follow along with this example, you should be familiar with the following C programming concepts:

  1. C if...else Statement.
  2. C for Loop.
  3. C while and do...while Loop.
  4. C break statement.
  5. C Continue statement.

Factors of a Positive Integer

This program accepts a positive integer from the user and displays all of the number's positive factors.

#include <stdio.h>
#include <conio.h>
void main() 
{
    int num, i;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    printf("Factors of %d are: ", num);
    for (i = 1; i <= num; ++i) 
    {
        if (num % i == 0) 
        {
            printf("%d ", i);
        }
    }
    getch();
}

Expected Output:-


Post a Comment

Previous Post Next Post