Posted on by Kalkicode
Code Number

# Find the sum of all even factors of a number

In this article, we will discuss how to find the sum of all even factors of a given number using a program. Factors of a number are the numbers that divide the given number without leaving a remainder. Even factors are those factors that are divisible by 2. For example, the even factors of 10 are 2 and 10, while the even factors of 20 are 2, 4, 10, and 20.

## Problem Statement

Given a positive integer, we want to find and sum all its even factors. If the input number is negative, we will not perform any computation and return immediately.

## Example

Let's take an example to understand the process of finding the sum of all even factors of a number. Consider the number 20.

• The factors of 20 are 1, 2, 4, 5, 10, and 20.
• The even factors are 2, 4, 10, and 20.
• The sum of these even factors is 2 + 4 + 10 + 20 = 36.

## Pseudocode

Below is the pseudocode to find the sum of all even factors of a number:

factor_sum(number):
if number < 0:
return
result = 0
if number is even:
result = number
for i = 2 to number / 2:
if i is even and number % i == 0:
add i to the list of even factors
result += i
print the list of even factors
print the sum of even factors (result)

## Algorithm Explanation

1. The factor_sum function takes the input number as its parameter.
2. It checks if the number is negative, and if so, it returns immediately without any further computation.
3. If the number is even, it initializes the result variable with the number itself, as the number is also an even factor of itself.
4. The function then iterates from 2 to number / 2 to find all the factors of the given number.
5. For each iteration, it checks if i is even and if it evenly divides the input number number.
6. If both conditions are true, it adds i to the list of even factors and also adds i to the result variable to keep track of the sum of even factors.
7. After the loop finishes, it prints the list of even factors and the sum of all even factors.

## Code Solution

Here given code implementation process.

//C Program
//Find the sum of all even factors of a number
#include <stdio.h>

void factor_sum(long long number)
{
if (number < 0)
{
return;
}
long long result = 0;
printf("Number :  %lld ", number);
printf("\nEven Factors : [");
if (number % 2 == 0)
{
result = number;
for (int i = 2; i <= number / 2; ++i)
{
//Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 && number % i == 0)
{
printf("%d,", i);
//Sum Factors
result += i;
}
}
printf("%lld", number);
}
printf("]\n");
printf("Factors sum : %lld\n\n", result);
}
int main()
{
//Test Cases
factor_sum(50);
factor_sum(180);
factor_sum(11);
factor_sum(20);
factor_sum(12);
return 0;
}

#### Output

Number :  50
Even Factors : [2,10,50]
Factors sum : 62

Number :  180
Even Factors : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum : 468

Number :  11
Even Factors : []
Factors sum : 0

Number :  20
Even Factors : [2,4,10,20]
Factors sum : 36

Number :  12
Even Factors : [2,4,6,12]
Factors sum : 24
/*
Java program
Find the sum of all even factors of a number
*/
public class MyNumber
{
public void factor_sum(long number)
{
long result = 0;
System.out.print("Number : " + number + " ");
System.out.print("\nFactors : [");
if (number % 2 == 0)
{
result = number;
for (int i = 1; i <= number / 2; ++i)
{
//Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 && number % i == 0)
{
System.out.print(i + ",");
//Sum Factors
result += i;
}
}
//Given number is itself factor
System.out.print(number);
}
System.out.print("]\n");
System.out.print("Factors sum : " + result + "\n\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.factor_sum(50);
obj.factor_sum(180);
obj.factor_sum(11);
obj.factor_sum(20);
obj.factor_sum(12);
}
}

#### Output

Number :  50
Even Factors : [2,10,50]
Factors sum : 62

Number :  180
Even Factors : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum : 468

Number :  11
Even Factors : []
Factors sum : 0

Number :  20
Even Factors : [2,4,10,20]
Factors sum : 36

Number :  12
Even Factors : [2,4,6,12]
Factors sum : 24
/*
C++ program
Find the sum of all even factors of a number
*/
#include<iostream>

using namespace std;
class MyNumber
{
public: void factor_sum(long number)
{
long result = 0;
cout << "Number : " << number << " ";
cout << "\nFactors : [";
if (number % 2 == 0)
{
result = number;
for (int i = 1; i <= number / 2; ++i)
{
//Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 && number % i == 0)
{
cout << i << ",";
//Sum Factors
result += i;
}
}
cout << number;
}
cout << "]\n";
cout << "Factors sum : " << result << "\n\n";
}
};
int main()
{
MyNumber obj =  MyNumber();
//Test Cases
obj.factor_sum(50);
obj.factor_sum(180);
obj.factor_sum(11);
obj.factor_sum(20);
obj.factor_sum(12);
return 0;
}

#### Output

Number : 50
Factors : [2,10,50]
Factors sum : 62

Number : 180
Factors : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum : 468

Number : 11
Factors : []
Factors sum : 0

Number : 20
Factors : [2,4,10,20]
Factors sum : 36

Number : 12
Factors : [2,4,6,12]
Factors sum : 24
/*
C# program
Find the sum of all even factors of a number
*/
using System;
public class MyNumber
{
public void factor_sum(long number)
{
long result = 0;
Console.Write("Number : " + number + " ");
Console.Write("\nFactors : [");
if (number % 2 == 0)
{
result = number;
for (int i = 1; i <= number / 2; i++)
{
//Check [i] is Even and its divisible by number reMainder is zero
if (i % 2 == 0 && number % i == 0)
{
Console.Write(i + ",");
//Sum Factors
result += i;
}
}
Console.Write(number);
}
Console.Write("]\n");
Console.Write("Factors sum : " + result + "\n\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.factor_sum(50);
obj.factor_sum(180);
obj.factor_sum(11);
obj.factor_sum(20);
obj.factor_sum(12);
}
}

#### Output

Number : 50
Factors : [2,10,50]
Factors sum : 62

Number : 180
Factors : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum : 468

Number : 11
Factors : []
Factors sum : 0

Number : 20
Factors : [2,4,10,20]
Factors sum : 36

Number : 12
Factors : [2,4,6,12]
Factors sum : 24
<?php
/*
Php program
Find the sum of all even factors of a number
*/
class MyNumber
{
public 	function factor_sum(\$number)
{
\$result = 0;
echo("Number : ". \$number ." ");
echo("\nFactors : [");
if (\$number % 2 == 0)
{
\$result = \$number;
for (\$i = 1; \$i <= intval(\$number / 2); ++\$i)
{
//Check [i] is Even and its divisible by number remainder is zero
if (\$i % 2 == 0 && \$number % \$i == 0)
{
echo(\$i .",");
//Sum Factors
\$result += \$i;
}
}
//Given number is itself factor
echo(\$number);
}
echo("]\n");
echo("Factors sum : ". \$result ."\n\n");
}
}

function main()
{
\$obj = new MyNumber();
//Test Cases
\$obj->factor_sum(50);
\$obj->factor_sum(180);
\$obj->factor_sum(11);
\$obj->factor_sum(20);
\$obj->factor_sum(12);
}
main();

#### Output

Number : 50
Factors : [2,10,50]
Factors sum : 62

Number : 180
Factors : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum : 468

Number : 11
Factors : []
Factors sum : 0

Number : 20
Factors : [2,4,10,20]
Factors sum : 36

Number : 12
Factors : [2,4,6,12]
Factors sum : 24
/*
Node Js program
Find the sum of all even factors of a number
*/
class MyNumber
{
factor_sum(number)
{
var result = 0;
process.stdout.write("Number : " + number + " ");
process.stdout.write("\nFactors : [");
if (number % 2 == 0)
{
result = number;
for (var i = 1; i <= parseInt(number / 2); ++i)
{
//Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 && number % i == 0)
{
process.stdout.write(""+ i + ",");
//Sum Factors
result += i;
}
}
//Given number is itself factor
process.stdout.write(""+number);
}
process.stdout.write("]\n");
process.stdout.write("Factors sum : " + result + "\n\n");
}
}

function main(args)
{
var obj = new MyNumber();
//Test Cases
obj.factor_sum(50);
obj.factor_sum(180);
obj.factor_sum(11);
obj.factor_sum(20);
obj.factor_sum(12);
}
main();

#### Output

Number : 50
Factors : [2,10,50]
Factors sum : 62

Number : 180
Factors : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum : 468

Number : 11
Factors : []
Factors sum : 0

Number : 20
Factors : [2,4,10,20]
Factors sum : 36

Number : 12
Factors : [2,4,6,12]
Factors sum : 24
#   Python 3 program
#   Find the sum of all even factors of a number

class MyNumber :
def factor_sum(self, number) :
result = 0
print("Number : ", number ," ", end = "")
print("\nFactors : [", end = "")
if (number % 2 == 0) :
result = number
i = 1
while (i <= int(number / 2)) :
# Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 and number % i == 0) :
print(i ,",", end = "")
# Sum Factors
result += i

i += 1

print(number, end = "")

print("]\n", end = "")
print("Factors sum : ", result ,"\n\n", end = "")

def main() :
obj = MyNumber()
# Test Cases
obj.factor_sum(50)
obj.factor_sum(180)
obj.factor_sum(11)
obj.factor_sum(20)
obj.factor_sum(12)

if __name__ == "__main__": main()

#### Output

Number :  50
Factors : [2 ,10 ,50]
Factors sum :  62

Number :  180
Factors : [2 ,4 ,6 ,10 ,12 ,18 ,20 ,30 ,36 ,60 ,90 ,180]
Factors sum :  468

Number :  11
Factors : []
Factors sum :  0

Number :  20
Factors : [2 ,4 ,10 ,20]
Factors sum :  36

Number :  12
Factors : [2 ,4 ,6 ,12]
Factors sum :  24
#   Ruby program
#   Find the sum of all even factors of a number

class MyNumber

def factor_sum(number)

result = 0
print("Number  : ", number ," ")
print("\nFactors  : [")
if (number % 2 == 0)

result = number
i = 1
while (i <= number / 2)

# Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 && number % i == 0)

print(i ,",")
# Sum Factors
result += i
end
i += 1
end
print(number)
end
print("]\n")
print("Factors sum  : ", result ,"\n\n")
end
end
def main()

obj = MyNumber.new()
# Test Cases
obj.factor_sum(50)
obj.factor_sum(180)
obj.factor_sum(11)
obj.factor_sum(20)
obj.factor_sum(12)
end
main()

#### Output

Number  : 50
Factors  : [2,10,50]
Factors sum  : 62

Number  : 180
Factors  : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum  : 468

Number  : 11
Factors  : []
Factors sum  : 0

Number  : 20
Factors  : [2,4,10,20]
Factors sum  : 36

Number  : 12
Factors  : [2,4,6,12]
Factors sum  : 24

/*
Scala program
Find the sum of all even factors of a number
*/
class MyNumber
{
def factor_sum(number: Long): Unit = {
var result: Long = 0;
print("Number : " + number + " ");
print("\nFactors : [");
if (number % 2 == 0)
{
result = number;
var i: Int = 1;
while (i <= (number / 2).toInt)
{
//Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 && number % i == 0)
{
print(""+ i + ",");
//Sum Factors
result += i;
}
i += 1;
}
print(number);
}
print("]\n");
print("Factors sum : " + result + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Cases
obj.factor_sum(50);
obj.factor_sum(180);
obj.factor_sum(11);
obj.factor_sum(20);
obj.factor_sum(12);
}
}

#### Output

Number : 50
Factors : [2,10,50]
Factors sum : 62

Number : 180
Factors : [2,4,6,10,12,18,20,30,36,60,90,180]
Factors sum : 468

Number : 11
Factors : []
Factors sum : 0

Number : 20
Factors : [2,4,10,20]
Factors sum : 36

Number : 12
Factors : [2,4,6,12]
Factors sum : 24
/*
Swift program
Find the sum of all even factors of a number
*/
class MyNumber
{
func factor_sum(_ number: Int)
{
var result: Int = 0;
print("Number : ", number ," ", terminator: "");
print("\nFactors : [", terminator: "");
if (number % 2 == 0)
{
result = number;
var i: Int = 1;
while (i <= number / 2)
{
//Check [i] is Even and its divisible by number remainder is zero
if (i % 2 == 0 && number % i == 0)
{
print(i ,",", terminator: "");
//Sum Factors
result += i;
}
i += 1;
}
print(number, terminator: "");
}
print("]\n", terminator: "");
print("Factors sum : ", result ,"\n\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Cases
obj.factor_sum(50);
obj.factor_sum(180);
obj.factor_sum(11);
obj.factor_sum(20);
obj.factor_sum(12);
}
main();

#### Output

Number :  50
Factors : [2 ,10 ,50]
Factors sum :  62

Number :  180
Factors : [2 ,4 ,6 ,10 ,12 ,18 ,20 ,30 ,36 ,60 ,90 ,180]
Factors sum :  468

Number :  11
Factors : []
Factors sum :  0

Number :  20
Factors : [2 ,4 ,10 ,20]
Factors sum :  36

Number :  12
Factors : [2 ,4 ,6 ,12]
Factors sum :  24

## Time Complexity

The time complexity of this algorithm is O(N), where N is the input number. This is because the function iterates from 2 to number / 2 to find all factors, and in the worst case, when the input number is prime, the loop runs N/2 times.

## Output Explanation

Let's analyze the output of the provided C program for the given test cases:

• factor_sum(50):

• Even Factors: [2, 10, 50]
• Sum of Even Factors: 62
• factor_sum(180):

• Even Factors: [2, 4, 6, 10, 12, 18, 20, 30, 36, 60, 90, 180]
• Sum of Even Factors: 468
• factor_sum(11):

• Even Factors: []
• Sum of Even Factors: 0
• factor_sum(20):

• Even Factors: [2, 4, 10, 20]
• Sum of Even Factors: 36
• factor_sum(12):

• Even Factors: [2, 4, 6, 12]
• Sum of Even Factors: 24

The program correctly finds the even factors of each input number and calculates their sum as expected.

## 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