English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C program displays number factors

Comprehensive Collection of C Programming Examples

In this example, you will learn to find all the factors of the integer entered by the user.

To understand this example, you should understand the followingC programmingTopic:

This program takes a positive integer from the user and displays all the positive factors of the number.

Find the factors of a positive integer

#include <stdio.h>
int main() {
    int num, i;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    printf("%d's factors: ", num);
    for (i = 1; i <= num; ++i) {
        if (num % i == 0) {
            printf("%d ", i);
        }
    }
    return 0;
}

Output result

Enter a positive integer: 60
6Factors of 0: 1 2 3 4 5 6 10 12 15 20 30 60

In the program, the positive integer entered by the user is stored in num.

Iterate the for loop until i <= num is false.

In each iteration, it checks whether the number can be divided by i. This is the condition for i to become a factor of num.

if (num % i == 0) {
            printf("%d ", i);
}

Then, the value of i increases1.

Comprehensive Collection of C Programming Examples