Display Factors of a Number in C++
C++ Program to Display Factors of a Number (5 Methods).
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}
Output
Enter a number: 24 Factors of 24 are: 1 2 3 4 6 8 12 24
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:
cin >> num;
The user enters a number, for example: 24
1cout << "Enter a number: ";
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:
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
1cout << "Factors of " << num << " are: ";
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);
}
}
}
1// 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
-
Basic Loop (1 to n)
: Best for learning - simple and clear.
-
Optimized (up to √n)
: Best for efficiency - recommended for large numbers.
-
Vectors
: Best for flexibility - dynamic size, easy to use.
-
Arrays
: Good when maximum size is known - fixed memory.
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
: