Factors of a Natural Number

Program to find all factors of a number

BeginnerTopic: Loop Programs
Back

C++ Factors of a Natural Number Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int num;
    
    cout << "Enter a number: ";
    cin >> num;
    
    cout << "Factors of " << num << " are: ";
    
    for (int i = 1; i <= num; i++) {
        if (num % i == 0) {
            cout << i << " ";
        }
    }
    cout << endl;
    
    return 0;
}
Output
Enter a number: 24
Factors of 24 are: 1 2 3 4 6 8 12 24

Understanding Factors of a Natural Number

This program finds all factors (divisors) of a given number. A factor is an integer that divides the number evenly without leaving a remainder. Finding factors is fundamental in number theory, prime factorization, and many mathematical and programming problems.

---

1. What are Factors?

Factors (also called divisors) of a number are all integers that divide it evenly.

Mathematical definition:

If a divides b evenly (b % a == 0), then a is a factor of b

Examples:

Factors of 24:

1, 2, 3, 4, 6, 8, 12, 24
Verification: 24 ÷ 1 = 24, 24 ÷ 2 = 12, 24 ÷ 3 = 8, etc.

Factors of 12:

1, 2, 3, 4, 6, 12

Factors of 7 (prime number):

1, 7 (only two factors)

Properties:

Every number has at least two factors: 1 and itself
Prime numbers have exactly two factors
Factors always come in pairs (except perfect squares)

---

2. Header File: #include <iostream>

#include <iostream>

Provides:

cout → for displaying output
cin → for reading input

---

3. Declaring Variables

int num;

Stores the number for which we'll find factors
Integer type is appropriate

---

4. Taking Input From User

`cout << "Enter a number: ";`

cin >> num;

Prompts user to enter a number
Reads and stores it

Example:

User enters:

24

num = 24

---

5. Displaying Output Header

`cout << "Factors of " << num << " are: ";`

This prints:

Text: "Factors of "
Number: 24
Text: " are: "

Output:

Factors of 24 are:

---

6. Finding All Factors

for (int i = 1; i <= num; i++)

if (num % i == 0)

cout << i << " ";

How it works:

We iterate through all numbers from 1 to num and check if each divides num evenly.

Step-by-step (for num = 24):

Iteration 1 (i = 1):

Check: 24 % 1 == 00 == 0

true

Print:

"1 "

Iteration 2 (i = 2):

Check: 24 % 2 == 00 == 0

true

Print:

"2 "

Iteration 3 (i = 3):

Check: 24 % 3 == 00 == 0

true

Print:

"3 "

Iteration 4 (i = 4):

Check: 24 % 4 == 00 == 0

true

Print:

"4 "

Iteration 5 (i = 5):

Check: 24 % 5 == 04 == 0

false

Skip (don't print)

Iteration 6 (i = 6):

Check: 24 % 6 == 00 == 0

true

Print:

"6 "

... continue through all iterations ...

Iteration 24 (i = 24):

Check: 24 % 24 == 00 == 0

true

Print:

"24 "

Final output:

"1 2 3 4 6 8 12 24 "

---

7. Understanding the Modulo Check

num % i == 0

What this means:

num % i gives the remainder when num is divided by i
If remainder is 0, then i divides num evenly
Therefore, i is a factor of num

Examples:

24 % 1 = 0 → 1 is a factor ✅
24 % 2 = 0 → 2 is a factor ✅
24 % 3 = 0 → 3 is a factor ✅
24 % 5 = 4 → 5 is NOT a factor ❌

---

8. Complete Example Walkthrough

Input:

num = 24

Checking each number from 1 to 24:

i = 1: 24 % 1 = 0 →

Factor

→ Print "1 "

i = 2: 24 % 2 = 0 →

Factor

→ Print "2 "

i = 3: 24 % 3 = 0 →

Factor

→ Print "3 "

i = 4: 24 % 4 = 0 →

Factor

→ Print "4 "

i = 5: 24 % 5 = 4 → Not factor → Skip
i = 6: 24 % 6 = 0 →

Factor

→ Print "6 "

i = 7: 24 % 7 = 3 → Not factor → Skip
i = 8: 24 % 8 = 0 →

Factor

→ Print "8 "

i = 9: 24 % 9 = 6 → Not factor → Skip
i = 10: 24 % 10 = 4 → Not factor → Skip
i = 11: 24 % 11 = 2 → Not factor → Skip
i = 12: 24 % 12 = 0 →

Factor

→ Print "12 "

i = 13-23: All have remainders → Skip
i = 24: 24 % 24 = 0 →

Factor

→ Print "24 "

Result:

Factors are 1, 2, 3, 4, 6, 8, 12, 24 ✅

---

9. Properties of Factors

Factor pairs:

Factors come in pairs that multiply to the number
For 24: (1, 24), (2, 12), (3, 8), (4, 6)
1 × 24 = 24, 2 × 12 = 24, 3 × 8 = 24, 4 × 6 = 24

Perfect squares:

For perfect squares, one factor appears twice
Example: 16 = 4²
Factors: 1, 2, 4, 8, 16
Factor 4 appears once (not twice in the list, but 4 × 4 = 16)

Prime numbers:

Prime numbers have exactly two factors: 1 and itself
Example: 7 has factors: 1, 7

---

10. Optimization (Advanced)

Current approach:

Check all numbers from 1 to n

Time complexity: O(n)

Optimized approach:

Check only up to √n

Find factors in pairs
Time complexity: O(√n)
More efficient for large numbers

Example optimized code:

for (int i = 1; i <= sqrt(num); i++) {

if (num % i == 0) {

if (i != num / i) {

cout << num / i << " "; // Pair factor

}

}

}

        cout << i << " ";           // First factor

Our program uses the simpler approach

which is easier to understand for beginners.

---

11. Edge Cases

Case 1: Number 1

Factors: 1
Only one factor (itself)

Case 2: Prime numbers

Example: 7
Factors: 1, 7
Exactly two factors

Case 3: Perfect squares

Example: 16
Factors: 1, 2, 4, 8, 16
One factor (4) is the square root

Case 4: Number 0

0 has infinite factors (every number divides 0)
Program would need special handling

---

12. Real-World Applications

Prime factorization:

Finding factors is first step in prime factorization
Example: 24 = 2³ × 3 (prime factors)

Number theory:

Studying properties of numbers
Finding divisors for mathematical proofs

Programming:

Checking divisibility
Finding common factors (GCD)
Many algorithmic problems

---

Summary

Factors are all integers that divide a number evenly
Algorithm: Check each number from 1 to n using modulo operator
If num % i == 0, then i is a factor
Print all factors found
Time complexity: O(n) - can be optimized to O(√n)

This program teaches:

Modulo operator for divisibility checking
Iteration through all possibilities
Finding divisors of a number
Basic number theory concepts

Understanding factors helps in:

Prime factorization
Finding GCD and LCM
Number theory problems
Many mathematical programming challenges

Finding factors is a fundamental operation in number theory, and this program demonstrates a straightforward approach that's perfect for beginners learning about divisors and number properties.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Factors of a Natural Number

This C++ program is part of the "Loop Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents