
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all combinations of factors in C++
In this problem, we are given a number n. Our task is to print all combinations of factors of n.
Let’s take an example to understand the topic better −
Input: 24 Output: 2 2 2 3 2 4 3 8 3 4 6 2 12
For this, we will use recursion function which will find a combination of factors of the number. And we will store all our combinations in an array of array.
Example
This code will show the implementation of our solution.
#include<bits/stdc++.h> using namespace std; vector<vector<int>> factor_Combo; void genreateFactorCombinations(int first, int eachFactor, int n, vector<int>factor) { if (first>n || eachFactor>n) return; if (eachFactor == n){ factor_Combo.push_back(factor); return; } for (int i = first; i < n; i++) { if (i*eachFactor>n) break; if (n % i == 0){ factor.push_back(i); genreateFactorCombinations(i, i*eachFactor, n, factor); factor.pop_back(); } } } void printcombination() { for (int i = 0; i < factor_Combo.size(); i++){ for (int j = 0; j < factor_Combo[i].size(); j++) cout<<factor_Combo[i][j]<<"\t"; cout<<endl; } } int main() { int n = 24; vector<int>single_result_list; cout<<"All Factor combinations of "<<n<<" are :\n"; genreateFactorCombinations(2, 1, n, single_result_list); printcombination(); return 0; }
Output
All Factor combinations of 24 are − 2 2 2 3 2 2 6 2 3 4 2 12 3 8 4 6
- Related Articles
- Print all combinations of balanced parentheses in C++
- Print all the combinations of a string in lexicographical order in C++
- Print all combinations of points that can compose a given number in C++
- Print all prime factors and their powers in C++
- Print all possible combinations of r elements in a given array of size n in C++
- C Program for efficiently print all prime factors of a given number?
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- All combinations of sums for array in JavaScript
- Generate all combinations of supplied words in JavaScript
- All pair combinations of 2 tuples in Python
- Golang Program to Read Three Digits and Print all Possible Combinations from the Digits
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Python – All combinations of a Dictionary List
- All combinations of strings that can be used to dial a number in C/C++?

Advertisements