C Programming Examples Tutorial Index

C Loop Programs

In this C programming tutorial, you will learn how to find the factors of a user-entered integer number.



What Is Meant by the Factor of a Number?

The factor of a number refers to any whole number (positive or negative) that can be divided evenly into another number without producing a remainder is called a "factor of a number."

For example, the factors of the number 12 are 1, 2, 3, 4, 6, and 12. These numbers divide evenly into 12 and can be multiplied to recreate the original value.

Here are the equations representing all the factors of 12:

1 × 12 = 12
2 × 6 = 12
3 × 4 = 12
4 × 3 = 12
6 × 2 = 12
12 × 1 = 12

C Program to Find Factors of a Positive Integer

#include <stdio.h>

int main() {
    int number, j;

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

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

    return 0;
}

Program Output:

Enter any positive integer: 12
Factors of the given number 12 are: 1 2 3 4 6 12

Enter any positive integer: 50
Factors of the given number 50 are: 1 2 5 10 25 50

Program Explanation:

  1. The program prompts the user to enter any positive integer.
  2. The input is read and stored in the variable "number".
  3. The program then enters a loop that iterates from "1" to the value of the "number".
  4. Within the loop, it checks if the "number" is divisible evenly by the current value of "j".
  5. If the condition is satisfied, "j" is printed because it is a factor of the "number."
  6. The loop continues until all possible factors have been checked.
  7. The program finishes when the loop ends.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram