C++ Program to Find the Factors of a Number
Program to find Factors of a number
To find the answer to a number we will use a loop in which we start dividing the number with 1 up to the number itself and the numbers which perfectly divides the number are the factors.
For Example 100
Factors are: 1, 2, 4, 5, 10, 20, 25, 50, 100
Different methods
Following are the methods that we will discuss in this post –
- Linear traversal between [1, num] to find factors
- Linear traversal between [1, √num] to find factors
- Using Vector to solve issues caused by the above method
- 2 Loop approach to solve without vectors
Method 1
For a user input num
- Using loop in the iteration of (i) traverse from [1, num]
- Print all values of (i) that divide num perfectly (num % i == 0)
Method 1 Code:-
Run
//C++ Program Factors of a number
#include <bits/stdc++.h>
using namespace std;
//main Program
int main()
{
int num;
num=100;
cout << "Factors of " << num << " are: " << endl;
// finding and printing factors b/w 1 to num
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
cout << i << " ";
}
}
// Time Complexity: O(N)
// Space Complexity: O(1)Output
Factors of 100 are: 1 2 4 5 10 20 25 50 100
Method 2
This method uses observations that all factors come in pairs.
All Factors come in pairs
For n = a * b (For each a there exists a unique b)
Example : 100
(1,100), (2, 50), (4, 25), (5, 20), (10, 100)
Example : 100
(1,100), (2, 50), (4, 25), (5, 20), (10, 100)
Shorten the lopp
We can shorten the loop running between [1, num] to [1, √num]
Since we will find all pairs before √num (n = sqrt(n) * sqrt(n))
Since we will find all pairs before √num (n = sqrt(n) * sqrt(n))
Method 2 Code:-
Run
//C++ Program Factors of a number
#include <bits/stdc++.h>
using namespace std;
void getFactors(int num){
for(int i = 1; i <= sqrt(num); i++)
{
if (num % i == 0){
// Example (10,10) we only want to print once
// If both factors are equal then print just one
if(i == num/i)
cout << i << " ";
// other wise print both
else
cout << i << " " << num/i << " ";
}
}
}
//main Program
int main()
{
int num =100;
getFactors(num);
}
// Time Complexity: O(√N)
// Space Complexity: O(1)
// Issue : Doesn't print in ascending orderOutput
1 100 2 50 4 25 5 20 10
Issue
As we can see the factors are not printed in ascending order. We can solve this below with new methods
Method 3
This method uses knowledge of vectors.
- Use the previous general approach
- Delay printing of larger pair value by not printing it and adding it to a vector
- Print the vector at the end in reverse order.
Method 3 Code:-
Run
//C++ Program Factors of a number
#include <bits/stdc++.h>
using namespace std;
void getFactors(int num){
// Vector to store larger pair value
vector v;
for(int i = 1; i <= sqrt(num); i++)
{
if (num % i == 0)
{
// Example (10,10) we only want to print once
// If both factors are equal then print just one
if(i == num/i)
cout << i << " ";
// other wise print both
else
{
cout << i << " "; // larger pair pushed, so can be printed later v.push_back(num / i); } } } // print vector in reverse for (int i = v.size() - 1; i >= 0; i--) printf("%d ", v[i]); } //main Program int main() { int num; cout << "Enter a positive number: "; cin >> num; getFactors(num); } // Time Complexity: O(√N) // Auxilary Space Complexity: O(√N)
Output
Enter a positive number: 100
1 2 4 5 10 20 25 50 100
Issue
Takes extra space, we can handle this with method below
Method 4
This method uses another decrementing for loop and doesn’t use any extra space as well.
Method 4 Code:-
Run
//C++ Program Factors of a number
#include<bits/stdc++.h>
using namespace std;
void getFactors(int num){
// Same i used in other for loop
int i;
// to avoid double printing
int flag = false;
for(i = 1; i <= sqrt(num); i++)
{
if (num % i == 0)
cout << i << " ";
// To avoid double printing of equal pairs
// Example (10,10) we only want to print once
if(i == num/i)
flag = true;
}
// if flag is true then we had double pairs like (10,10)
// we should do i-- so as not to do double printing of pair divisor
// doing i -=2 rather than i-- as in previous for loop we exited
// with i++, example, i = 10 became 11 and we need to start with 9
// so as to ignore 10 as its a double pair
if(flag)
i -= 2;
// printing pairs
for(;i>=1;i--) { if (num % i == 0) cout << num/i << " "; } } //main Program int main() { int num; cout << "Enter a positive number: ";
num=100;
getFactors(num);
}
// Time Complexity: O(√N)
// Space Complexity: O(1)Output
1 2 4 5 10 20 25 50 100
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python

In case of 2,6,12,20… num is divided by sqrt(num)+1 which is equal to i after 1st for loop
so solution is
#include
#include
using namespace std;
int main()
{
int num,i;
bool repeat=false;
cout<<"enter number"<>num;
for(i=1;i<=sqrt(num);i++)
{
if(num%i==0)
cout<<i<=1;i–)
{
if(num%i==0)
cout<<num/i<<" ";
}
return (0);
}