Posted on by Kalkicode
Code Number

# Print all factors of number in pair

The given problem is about finding and printing all factor pairs of a given number. A factor pair of a number "num" consists of two numbers, say "a" and "b," such that "a * b = num." In other words, both "a" and "b" are divisors of "num," and when multiplied together, they give the original number.

For example, for the number 10, the factor pairs are (1, 10) and (2, 5) because 1 * 10 = 10 and 2 * 5 = 10.

## Problem Statement & Explanation with Suitable Example

The task is to write a C program that takes a number as input and finds all the factor pairs of that number, printing them in the format "(a X b)." The program should loop through numbers from 1 to num/2 (as the factor pairs will be at most num/2), and whenever it finds a factor "a," it will print the corresponding factor "b" as "num / a."

For example, consider the number 136:

• The loop will iterate through numbers 1 to 68 (136/2) and find the factor pairs (1, 136), (2, 68), (4, 34), (8, 17), (17, 8), (34, 4), and (68, 2).

## Standard Pseudocode & Algorithm with Proper Explanation

``````function factorPairs(num):
Print "Factors pair of a number num is"
for i from 1 to num / 2 do:
if num is divisible by i then:
Print "(i X num / i)"

function main():
Call factorPairs(136)
Call factorPairs(760)
Call factorPairs(22)
Call factorPairs(23)
``````

Let's represent the function to find factor pairs as `factorPairs(num)`.

1. Start the function `factorPairs(num)`:
2. Print a message to indicate the number for which factor pairs are being calculated.
3. Execute a loop from `i = 1` to `num / 2`.
4. Within the loop, check if `num` is divisible by `i` (i.e., `num % i == 0`).
5. If `num` is divisible by `i`, then it means `i` is a factor of `num`.
6. Print the factor pair as `(i X num / i)`.

## Code Solution

Here given code implementation process.

``````// C Program
// Print all factors of number in pair
#include <stdio.h>

// Find all the factor pair of a number
void factorPairs(int num)
{
printf("\n Factors pair of a number %d is \n", num);
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (num % i == 0)
{
// When get resultant pair
printf(" (%d X %d) \n", i, num / i);
}
}
}
int main()
{
// Test Case
factorPairs(136);
factorPairs(760);
factorPairs(22);
factorPairs(23);
return 0;
}``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````
``````/*
Java Program
Print all factors of number in pair
*/
public class Factorization
{
// Find all the factor pair of a number
public void factorPairs(int num)
{
System.out.print("\n Factors pair of a number " + num + " is \n");
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (num % i == 0)
{
// When get resultant pair
System.out.print(" (" + i + " X " + num / i + ") \n");
}
}
}
public static void main(String[] args)
{
// Test Case
}
}``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Print all factors of number in pair
*/
class Factorization
{
public:
// Find all the factor pair of a number
void factorPairs(int num)
{
cout << "\n Factors pair of a number " << num << " is \n";
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (num % i == 0)
{
// When get resultant pair
cout << " (" << i << " X " << num / i << ") \n";
}
}
}
};
int main()
{
// Test Case
return 0;
}``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````
``````// Include namespace system
using System;
/*
C# Program
Print all factors of number in pair
*/
public class Factorization
{
// Find all the factor pair of a number
public void factorPairs(int num)
{
Console.Write("\n Factors pair of a number " + num + " is \n");
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (num % i == 0)
{
// When get resultant pair
Console.Write(" (" + i + " X " + num / i + ") \n");
}
}
}
public static void Main(String[] args)
{
// Test Case
}
}``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````
``````<?php
/*
Php Program
Print all factors of number in pair
*/
class Factorization
{
// Find all the factor pair of a number
public	function factorPairs(\$num)
{
echo "\n Factors pair of a number ". \$num ." is \n";
// Execute loop through by 1 to num/2
for (\$i = 1; \$i <= intval(\$num / 2); ++\$i)
{
if (\$num % \$i == 0)
{
// When get resultant pair
echo " (". \$i ." X ". intval(\$num / \$i) .") \n";
}
}
}
}

function main()
{
// Test Case
}
main();``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````
``````/*
Node Js Program
Print all factors of number in pair
*/
class Factorization
{
// Find all the factor pair of a number
factorPairs(num)
{
process.stdout.write("\n Factors pair of a number " + num + " is \n");
// Execute loop through by 1 to num/2
for (var i = 1; i <= parseInt(num / 2); ++i)
{
if (num % i == 0)
{
// When get resultant pair
process.stdout.write(" (" + i + " X " + parseInt(num / i) + ") \n");
}
}
}
}

function main()
{
// Test Case
}
main();``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````
``````#  Python 3 Program
#  Print all factors of number in pair

class Factorization :
#  Find all the factor pair of a number
def factorPairs(self, num) :
print("\n Factors pair of a number ", num ," is ")
i = 1
#  Execute loop through by 1 to num/2
while (i <= int(num / 2)) :
if (num % i == 0) :
#  When get resultant pair
print(" (", i ," X ", int(num / i) ,") ")

i += 1

def main() :
#  Test Case

if __name__ == "__main__": main()``````

#### Output

`````` Factors pair of a number  136  is
( 1  X  136 )
( 2  X  68 )
( 4  X  34 )
( 8  X  17 )
( 17  X  8 )
( 34  X  4 )
( 68  X  2 )

Factors pair of a number  760  is
( 1  X  760 )
( 2  X  380 )
( 4  X  190 )
( 5  X  152 )
( 8  X  95 )
( 10  X  76 )
( 19  X  40 )
( 20  X  38 )
( 38  X  20 )
( 40  X  19 )
( 76  X  10 )
( 95  X  8 )
( 152  X  5 )
( 190  X  4 )
( 380  X  2 )

Factors pair of a number  22  is
( 1  X  22 )
( 2  X  11 )
( 11  X  2 )

Factors pair of a number  23  is
( 1  X  23 )``````
``````#  Ruby Program
#  Print all factors of number in pair

class Factorization
#  Find all the factor pair of a number
def factorPairs(num)
print("\n Factors pair of a number ", num ," is \n")
i = 1
#  Execute loop through by 1 to num/2
while (i <= num / 2)
if (num % i == 0)
#  When get resultant pair
print(" (", i ," X ", num / i ,") \n")
end

i += 1
end

end

end

def main()
#  Test Case
end

main()``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)
``````
``````/*
Scala Program
Print all factors of number in pair
*/
class Factorization
{
// Find all the factor pair of a number
def factorPairs(num: Int): Unit = {
print("\n Factors pair of a number " + num + " is \n");
var i: Int = 1;
// Execute loop through by 1 to num/2
while (i <= (num / 2).toInt)
{
if (num % i == 0)
{
// When get resultant pair
print(" (" + i + " X " + (num / i).toInt + ") \n");
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Factorization = new Factorization();
// Test Case
}
}``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````
``````/*
Swift 4 Program
Print all factors of number in pair
*/
class Factorization
{
// Find all the factor pair of a number
func factorPairs(_ num: Int)
{
print("\n Factors pair of a number ", num ," is ");
var i: Int = 1;
// Execute loop through by 1 to num/2
while (i <= num / 2)
{
if (num % i == 0)
{
// When get resultant pair
print(" (", i ," X ", num / i ,") ");
}
i += 1;
}
}
}
func main()
{
// Test Case
}
main();``````

#### Output

`````` Factors pair of a number  136  is
( 1  X  136 )
( 2  X  68 )
( 4  X  34 )
( 8  X  17 )
( 17  X  8 )
( 34  X  4 )
( 68  X  2 )

Factors pair of a number  760  is
( 1  X  760 )
( 2  X  380 )
( 4  X  190 )
( 5  X  152 )
( 8  X  95 )
( 10  X  76 )
( 19  X  40 )
( 20  X  38 )
( 38  X  20 )
( 40  X  19 )
( 76  X  10 )
( 95  X  8 )
( 152  X  5 )
( 190  X  4 )
( 380  X  2 )

Factors pair of a number  22  is
( 1  X  22 )
( 2  X  11 )
( 11  X  2 )

Factors pair of a number  23  is
( 1  X  23 )``````
``````/*
Kotlin Program
Print all factors of number in pair
*/
class Factorization
{
// Find all the factor pair of a number
fun factorPairs(num: Int): Unit
{
print("\n Factors pair of a number " + num + " is \n");
var i: Int = 1;
// Execute loop through by 1 to num/2
while (i <= num / 2)
{
if (num % i == 0)
{
// When get resultant pair
print(" (" + i + " X " + num / i + ") \n");
}
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````

#### Output

`````` Factors pair of a number 136 is
(1 X 136)
(2 X 68)
(4 X 34)
(8 X 17)
(17 X 8)
(34 X 4)
(68 X 2)

Factors pair of a number 760 is
(1 X 760)
(2 X 380)
(4 X 190)
(5 X 152)
(8 X 95)
(10 X 76)
(19 X 40)
(20 X 38)
(38 X 20)
(40 X 19)
(76 X 10)
(95 X 8)
(152 X 5)
(190 X 4)
(380 X 2)

Factors pair of a number 22 is
(1 X 22)
(2 X 11)
(11 X 2)

Factors pair of a number 23 is
(1 X 23)``````

## Resultant Output Explanation

The provided C program executes the function `factorPairs()` for four test cases: 136, 760, 22, and 23. The output is shown in the code comments and is explained as follows:

1. For 136:

• Factors pairs are (1 X 136), (2 X 68), (4 X 34), (8 X 17), (17 X 8), (34 X 4), and (68 X 2).
• These pairs represent all possible combinations of factors that multiply to give 136.
2. For 760:

• Factors pairs are (1 X 760), (2 X 380), (4 X 190), (5 X 152), (8 X 95), (10 X 76), (19 X 40), (20 X 38), (38 X 20), (40 X 19), (76 X 10), (95 X 8), (152 X 5), (190 X 4), and (380 X 2).
• Again, these pairs represent all possible combinations of factors that multiply to give 760.
3. For 22:

• Factors pairs are (1 X 22) and (2 X 11).
• As 22 is not a perfect square, it has two distinct factors.
4. For 23:

• Factors pairs consist of only (1 X 23).
• As 23 is a prime number, it has only two factors, 1 and itself.

## Time Complexity of the Code

The time complexity of the code is O(n), where n is the input number `num`. The function `factorPairs()` iterates through numbers from 1 to `num/2`, and the number of iterations depends on the value of `num`. Asymptotically, the loop runs approximately n/2 times, which simplifies to O(n) time complexity.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post