Find the sum of all even factors of a number

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


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.

New Comment







© 2021, kalkicode.com, All rights reserved