C++ Program to Find Factors of a Number
You can find factors of a number in C++ by looping from 1 to the number and checking if
number % i == 0, then printing i as a factor.Examples
Input6
OutputFactors of 6 are: 1 2 3 6
Input13
OutputFactors of 13 are: 1 13
Input1
OutputFactors of 1 are: 1
How to Think About It
To find factors of a number, think about all numbers from 1 up to that number. For each, check if it divides the number evenly using the remainder operator
%. If the remainder is zero, that number is a factor.Algorithm
1
Get the input number from the user.2
Start a loop from 1 to the input number.3
For each number in the loop, check if it divides the input number evenly using <code>number % i == 0</code>.4
If yes, print that number as a factor.5
End the loop after reaching the input number.Code
cpp
#include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; cout << "Factors of " << number << " are: "; for (int i = 1; i <= number; ++i) { if (number % i == 0) { cout << i << " "; } } cout << endl; return 0; }
Output
Enter a number: 6
Factors of 6 are: 1 2 3 6
Dry Run
Let's trace the input 6 through the code to find its factors.
1
Input number
User enters 6.
2
Start loop
Loop variable i starts at 1.
3
Check divisibility
Check if 6 % i == 0 for i = 1 to 6.
4
Print factors
Print i when condition is true: 1, 2, 3, 6.
| i | 6 % i | Is factor? |
|---|---|---|
| 1 | 0 | Yes |
| 2 | 0 | Yes |
| 3 | 0 | Yes |
| 4 | 2 | No |
| 5 | 1 | No |
| 6 | 0 | Yes |
Why This Works
Step 1: Loop through numbers
We check every number from 1 to the input number to see if it divides the number evenly.
Step 2: Use modulo operator
The % operator gives the remainder. If remainder is zero, the number divides evenly.
Step 3: Print factors
All numbers that divide evenly are printed as factors.
Alternative Approaches
Check factors up to square root
cpp
#include <iostream> #include <cmath> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; cout << "Factors of " << number << " are: "; for (int i = 1; i <= sqrt(number); ++i) { if (number % i == 0) { cout << i << " "; if (i != number / i) { cout << number / i << " "; } } } cout << endl; return 0; }
This method is faster for large numbers because it only checks up to the square root, printing both factors at once.
Store factors in a vector and sort
cpp
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int number; vector<int> factors; cout << "Enter a number: "; cin >> number; for (int i = 1; i <= number; ++i) { if (number % i == 0) { factors.push_back(i); } } sort(factors.begin(), factors.end()); cout << "Factors of " << number << " are: "; for (int f : factors) { cout << f << " "; } cout << endl; return 0; }
This approach collects factors first, then prints them sorted. Useful if you want to process factors later.
Complexity: O(n) time, O(1) space
Time Complexity
The program checks every number from 1 to n, so it runs in O(n) time.
Space Complexity
It uses constant extra space, O(1), as it prints factors directly without storing them.
Which Approach is Fastest?
Checking up to the square root of n reduces time to O(√n), which is faster for large numbers.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple loop 1 to n | O(n) | O(1) | Small to medium numbers |
| Loop up to sqrt(n) | O(√n) | O(1) | Large numbers, faster execution |
| Store and sort factors | O(n log n) | O(n) | When factors need further processing |
Use the modulo operator
% to check if a number divides evenly without remainder.Beginners often forget to include the number itself as a factor or start the loop from 0 causing division errors.