#include <stdio.h>
int main() {
int num, i;
// Asking the user for input
printf("Enter a positive integer: ");
scanf("%d", &num);
// Check all numbers from 1 to num to find factors
printf("Factors of %d are: ", num);
for (i = 1; i <= num; i++) {
// If num is divisible by i, then i is a factor
if (num % i == 0) {
printf("%d ", i);
}
}
return 0;
}
Explanation:
The program prints all numbers that divide num
evenly, which are its factors.
Variables:
num
: Stores the number entered by the user.
i
: Loop variable used to check divisibility.
User Input:
The user is prompted to enter a number, which is stored in num
.
Loop to Find Factors:
A for
loop runs from 1
to num
.
For each i
, the program checks if num % i == 0
.
If true, i
is a factor and is printed.
Output:
Enter a positive integer: 28
Factors of 28 are: 1 2 4 7 14 28