Factors of a Natural Number
Program to find all factors of a number
C++ Factors of a Natural Number Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
a divides b evenly (b % a == 0), then a is a factor of bExamples:
Factors of 24:
Factors of 12:
Factors of 7 (prime number):
Properties:
---
2. Header File: #include <iostream>
#include <iostream>
Provides:
cout → for displaying outputcin → for reading input---
3. Declaring Variables
int num;
---
4. Taking Input From User
cin >> num;
Example:
24
num = 24---
5. Displaying Output Header
This prints:
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):
24 % 1 == 0 → 0 == 0 →true
✅
"1 "
Iteration 2 (i = 2):
24 % 2 == 0 → 0 == 0 →true
✅
"2 "
Iteration 3 (i = 3):
24 % 3 == 0 → 0 == 0 →true
✅
"3 "
Iteration 4 (i = 4):
24 % 4 == 0 → 0 == 0 →true
✅
"4 "
Iteration 5 (i = 5):
24 % 5 == 0 → 4 == 0 →false
❌
Iteration 6 (i = 6):
24 % 6 == 0 → 0 == 0 →true
✅
"6 "
... continue through all iterations ...
Iteration 24 (i = 24):
24 % 24 == 0 → 0 == 0 →true
✅
"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 iExamples:
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:
Factor
→ Print "1 "
Factor
→ Print "2 "
Factor
→ Print "3 "
Factor
→ Print "4 "
Factor
→ Print "6 "
Factor
→ Print "8 "
Factor
→ Print "12 "
Factor
→ Print "24 "
Result:
Factors are 1, 2, 3, 4, 6, 8, 12, 24 ✅
---
9. Properties of Factors
Factor pairs:
Perfect squares:
Prime numbers:
---
10. Optimization (Advanced)
Current approach:
Check all numbers from 1 to n
Optimized approach:
Check only up to √n
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 factorOur program uses the simpler approach
which is easier to understand for beginners.
---
11. Edge Cases
Case 1: Number 1
Case 2: Prime numbers
Case 3: Perfect squares
Case 4: Number 0
---
12. Real-World Applications
Prime factorization:
Number theory:
Programming:
---
Summary
num % i == 0, then i is a factorThis program teaches:
Understanding factors helps in:
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.