Find all the factors of a number in the C programming language.

Here is a C program for finding all the factors of a number.

#include <stdio.h>

int main() {
    int num;
    
    printf("请输入一个正整数:");
    scanf("%d", &num);
    
    printf("%d 的所有因子为:", num);
    
    for (int i = 1; i <= num; i++) {
        if (num % i == 0) {
            printf("%d ", i);
        }
    }
    
    return 0;
}

In the program, start by collecting a positive integer input from the user using the scanf function. Then, use a for loop to iterate through each number from 1 to that number, checking if it can be divided evenly by the input number. If it can, output that number as a factor.

For example, if the user input is 12, the output will be: 1 2 3 4 6 12, which are all the factors of 12.

bannerAds