Display Factors of a Number in C++
Collect the factors of a number into a vector in C++ and print them. Compares a vector with direct printing and shows how to cut the search to the square root.
1#include <iostream>2#include <vector>3#include <algorithm>4using namespace std;56int main() {7 int num;89 cout << "Enter a number: ";10 cin >> num;1112 vector<int> factors;1314 for (int i = 1; i <= num; i++) {15 if (num % i == 0) {16 factors.push_back(i);17 }18 }1920 cout << "Factors of " << num << " are: ";21 for (int i = 0; i < factors.size(); i++) {22 cout << factors[i] << " ";23 }24 cout << endl;2526 return 0;27}
Sample Output
Factors of 24 are: 1 2 3 4 6 8 12 24
Vector contents in order.
In this one
- How push_back grows a vector
- Why storing factors is useful before printing
- How to loop over a vector by index
- How to find factors in O(√n)
How it works
- 1
Read 24.
- 2
Test i = 1 to 24 with 24 % i == 0.
- 3
push_back each factor.
- 4
Loop over the vector to print 1 2 3 4 6 8 12 24.
Edge cases
Input 0 or negative
Enter 0.
The loop never runs and the vector is empty, so only the label prints.
Signed/unsigned comparison
i < factors.size() compares int with size_t.
It works for small sizes, but compilers warn. Use size_t i or a range-based for loop.
What's going on
The program finds all factors of a given number. For example:
A factor is a number that divides the given number exactly (remainder = 0).
Example:
Header Files Used
Understanding Factors
Definition:
A factor (divisor) of a number n is an integer d such that n % d == 0.
Properties:
Example (for n = 24):
Declaring Variables
The program declares:
int num;
vector<int> factors;
Taking Input From the User
The program asks:
cout << "Enter a number: ";
cin >> num;
The user enters a number, for example: 24
Finding Factors Algorithm
The algorithm uses a loop to check all numbers:
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
factors.push_back(i);
}
}
How it works:
Step-by-step (for num = 24):
i = 1: 24 % 1 = 0 → factors: [1]
i = 2: 24 % 2 = 0 → factors: [1, 2]
i = 3: 24 % 3 = 0 → factors: [1, 2, 3]
i = 4: 24 % 4 = 0 → factors: [1, 2, 3, 4]
i = 5: 24 % 5 = 4 ≠ 0 → skip
i = 6: 24 % 6 = 0 → factors: [1, 2, 3, 4, 6]
i = 7: 24 % 7 = 3 ≠ 0 → skip
i = 8: 24 % 8 = 0 → factors: [1, 2, 3, 4, 6, 8]
...
i = 24: 24 % 24 = 0 → factors: [1, 2, 3, 4, 6, 8, 12, 24]
Displaying the Factors
The program displays all factors:
cout << "Factors of " << num << " are: ";
for (int i = 0; i < factors.size(); i++) {
cout << factors[i] << " ";
}
cout << endl;
Output:
Factors of 24 are: 1 2 3 4 6 8 12 24
Optimized Approach (Checking up to sqrt(n))
More Efficient Method:
Instead of checking all numbers from 1 to n, we can check only up to √n:
for (int i = 1; i * i <= num; i++) {
if (num % i == 0) {
factors.push_back(i);
if (i != num / i) {
factors.push_back(num / i);
}
}
}
Why it works:
Example (for num = 24):
Time Complexity:
Other Methods (Mentioned but not shown in code)
Method 2: Using Arrays
int factors[100];
int count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
factors[count++] = i;
}
}
Method 3: Using Two Loops (Separate Small and Large Factors)
vector<int> smallFactors, largeFactors;
for (int i = 1; i * i <= num; i++) {
if (num % i == 0) {
smallFactors.push_back(i);
if (i != num / i) {
largeFactors.insert(largeFactors.begin(), num / i);
}
}
}
// Combine: smallFactors + largeFactors
Method 4: Recursive Approach
void findFactors(int num, int i, vector<int>& factors) {
if (i > num) return;
if (num % i == 0) {
factors.push_back(i);
}
findFactors(num, i + 1, factors);
}
When to Use Each Method
Best Practice: Use optimized method with vectors for most cases.
Important Considerations
Edge Cases:
Perfect Squares:
Large Numbers:
Sorting Factors:
Common Use Cases
Number Theory:
Programming Problems:
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
factors.size() for 36?
Pick an answer to check it.
Question 2
What does push_back do?
Pick an answer to check it.
Questions people ask
Why use a vector instead of printing directly?
You can count, sort, or reuse the factors later.
How to count factors?
factors.size() after the loop.
Faster method?
Loop i while i * i <= num, add i and num / i, then sort.