cc
#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;
}
Explanation:
#include <stdio.h>
: Includes the standard input-output header file for input/output functions.int main() { ... }
: The main function where the program execution starts.int number, i;
: Declares two integer variablesnumber
andi
.printf("Enter a positive integer: ");
: Displays a message asking the user to enter a positive integer.scanf("%d", &number);
: Reads an integer input from the user and stores it in thenumber
variable.printf("Factors of %d are: ", number);
: Displays a message indicating the factors of the entered number.for (i = 1; i <= number; ++i) { ... }
: Starts a loop that iterates from 1 to the entered number.if (number % i == 0) { ... }
: Checks if the current value ofi
is a factor ofnumber
using the modulo operator%
.printf("%d ", i);
: Ifi
is a factor ofnumber
, it prints the value ofi
.
return 0;
: Indicates the successful execution of the program. Themain
function returns 0 to the operating system.