Program to print factors of a number in pairs (original) (raw)
Last Updated : 2 May, 2025
Given a number n, the task of the programmer is to print the factors of the number in such a way that they occur in pairs. A pair signifies that the product of the pair should result in the number itself.
**Examples:
Input : 24
Output : 124
212
38
46
Input : 50
Output : 150
225
5*10
The simplest approach for this program is that we run a loop from 1 to the square root of N and print and print 'i' and 'N%i' if the number N is dividing 'i'.
The mathematical reason why we run the loop till square root of N is given below:
If **a*b = N where **1 < a ≤ b < N
N = ab ≥ a^2 ⇔ a^2 ≤ N ⇔ a ≤ √N
C++ `
// CPP program to print prime factors in // pairs. #include using namespace std;
void printPFsInPairs(int n) { for (int i = 1; i * i <= n; i++) if (n % i == 0) cout << i << "*" << n / i << endl; }
// Driver code int main() { int n = 24; printPFsInPairs(n); return 0; }
Java
// Java program to print prime factors in // pairs. public class GEE {
static void printPFsInPairs(int n)
{
for (int i = 1; i * i <= n; i++)
if (n % i == 0)
System.out.println(i + "*" + n / i);
}
// Driver code
public static void main(String[] args)
{
int n = 24;
printPFsInPairs(n);
}}
Python
Python 3 program to print prime factors in
pairs.
def printPFsInPairs(n): for i in range(1, int(pow(n, 1 / 2))+1): if n % i == 0: print(str(i) +"*"+str(int(n / i)))
Driver code
n = 24 printPFsInPairs(n)
C#
// C# program to print prime factors in // pairs. using System; public class GEE {
static void printPFsInPairs(int n)
{
for (int i = 1; i * i <= n; i++)
if (n % i == 0)
Console.Write(i + "*" + n / i + "\n");
}
// Driver code
public static void Main()
{
int n = 24;
printPFsInPairs(n);
}}
JavaScript
PHP
`
**Time Complexity: O(sqrt(n)) where n is given number
**Auxiliary Space: O(1)
Approach#2: Using for loop
The program first defines a function print_factor_pairs that takes a positive integer num as input. The function generates all factors of num using a list comprehension that loops through all numbers from 1 to the square root of num, and appends the pair (i, num//i) to a list if num is divisible by i. The function then iterates through the list of factor pairs and prints them in the format "i*j". In the main code, the user inputs a number and calls the print_factor_pairs function with that number as input.
Algorithm
1. Define a function print_factor_pairs that takes a positive integer num as input.
2. Generate all factors of num using a list comprehension that loops through all numbers from 1 to the square root of num, and appends the pair (i, num//i) to a list if num is divisible by i.
3. Iterate through the list of factor pairs and print them in the format "i*j".
4. In the main code, input a number from the user and call the print_factor_pairs function with that number as input.
C++ `
// C++ program to print factors in pairs #include #include
using namespace std;
// Function to print factors in pairs void printFactorPairs(int num) { vector<pair<int, int>> factors; for (int i = 1; i * i <= num; i++) { if (num % i == 0) { int factor1 = i; int factor2 = num / i; factors.push_back(make_pair(factor1, factor2)); } }
// Print factor pairs
for (auto pair : factors) {
cout << pair.first << "*" << pair.second << endl;
}}
// Main code int main() { int num = 24; printFactorPairs(num);
return 0;} // This code is contributed by Shivam Tiwari
Java
import java.util.ArrayList; import java.util.AbstractMap.SimpleEntry; import java.util.List;
public class GFG { // Function to print factors in pairs public static void printFactorPairs(int num) { List<SimpleEntry<Integer, Integer>> factors = new ArrayList<>(); for (int i = 1; i * i <= num; i++) { if (num % i == 0) { int factor1 = i; int factor2 = num / i; factors.add(new SimpleEntry<>(factor1, factor2)); } }
// Print factor pairs
for (SimpleEntry<Integer, Integer> pair : factors) {
System.out.println(pair.getKey() + "*" + pair.getValue());
}
}
public static void main(String[] args) {
int num = 24;
printFactorPairs(num);
// This code is contributed by Shivam Tiwari
}}
Python
function to print factors in pairs
def print_factor_pairs(num): factors = [(i, num//i) for i in range(1, int(num*0.5)+1) if num % i == 0] for pair in factors: print(f"{pair[0]}{pair[1]}")
main code
num = 24 print_factor_pairs(num)
C#
using System; using System.Collections.Generic;
public class GFG { // Function to print factors in pairs public static void PrintFactorPairs(int num) { List<Tuple<int, int>> factors = new List<Tuple<int, int>>(); for (int i = 1; i * i <= num; i++) { if (num % i == 0) { int factor1 = i; int factor2 = num / i; factors.Add(Tuple.Create(factor1, factor2)); } }
// Print factor pairs
foreach (var pair in factors)
{
Console.WriteLine(pair.Item1 + "*" + pair.Item2);
}
}
// Main code
public static void Main()
{
int num = 24;
PrintFactorPairs(num);
// This code is contributed by Shivam Tiwari
}}
JavaScript
// Function to print factors in pairs function printFactorPairs(num) { // Create an empty array to store the factor pairs const factors = [];
// Loop through numbers from 1 to the square root of num
for (let i = 1; i * i <= num; i++) {
// Check if i is a factor of num
if (num % i === 0) {
// If i is a factor, calculate its pair factor
const factor1 = i;
const factor2 = num / i;
// Add the pair to the factors array
factors.push([factor1, factor2]);
}
}
// Print factor pairs
for (const [factor1, factor2] of factors) {
console.log(factor1 + "*" + factor2);
}}
// Main code // Input number for which factor pairs need to be found const num = 24;
// Call the function to print factor pairs
printFactorPairs(num);// This code is contributed by Shivam Tiwari
`
Time complexity: O(sqrt(num)) because the loop only goes up to the square root of num.
The time complexity of iterating through the list of factor pairs and printing them is O(sqrt(num)) because there are sqrt(num) factor pairs.
Space complexity: O(sqrt(num)) because the list of factor pairs generated by the program has at most sqrt(num) elements.