Find All Factors of a Natural Number
The program finds and prints all positive factors of a given natural number. A factor divides the number without leaving a remainder. For example, factors of 12 are 1, 2, 3, 4, 6 and 12. We test each integer from 1 up to the number and print it if it divides evenly.
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factors of %d are: ", n);
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Enter a positive integer: 12
Factors of 12 are: 1 2 3 4 6 12
Each integer from 1 to n is tested using the modulus operator. When n % i equals zero, i divides n exactly and is printed as a factor. This simple approach lists all divisors of the number.
