Display Factors of a Number
C++ Program to Display Factors of a Number (5 Methods)
C++ Display Factors of a Number Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
vector<int> factors;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
factors.push_back(i);
}
}
cout << "Factors of " << num << " are: ";
for (int i = 0; i < factors.size(); i++) {
cout << factors[i] << " ";
}
cout << endl;
return 0;
}Enter a number: 24 Factors of 24 are: 1 2 3 4 6 8 12 24
Understanding Display Factors of a Number
This program teaches you how to find and display all factors (divisors) of a number in C++. 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 This Program Does
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:
---
2. Header Files Used
---
3. Understanding Factors
Definition
:
A factor (divisor) of a number n is an integer d such that n % d == 0.
Properties
:
Example
(for n = 24):
---
4. Declaring Variables
The program declares:
int num;
vector<int> factors;
---
5. Taking Input From the User
The program asks:
cin >> num;
---
cout << "Enter a number: ";6. 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:
i = 2:
i = 3:
i = 4:
i = 5:
24 % 5 = 4 ≠ 0 → skip
i = 6:
i = 7:
24 % 7 = 3 ≠ 0 → skip
i = 8:
...
i = 24:
---
7. Displaying the Factors
The program displays all factors:
for (int i = 0; i < factors.size(); i++) {
cout << factors[i] << " ";
}
cout << endl;
Output:
---
cout << "Factors of " << num << " are: ";8. 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
:
---
9. 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 + largeFactorsMethod 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);
}
---
10. 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.
---
11. Important Considerations
Edge Cases
:
Perfect Squares
:
Large Numbers
:
Sorting Factors
:
---
12. Common Use Cases
Number Theory
:
Programming Problems
:
Real-World Applications
:
---
13. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning number theory, understanding loops and conditionals, and preparing for prime factorization, GCD/LCM calculations, and more advanced number theory problems in C++ programs.
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 Display Factors of a Number
This C++ program is part of the "Array Operations 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.