Find the sum of factors of a given number

Here given code implementation process.

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

void factor_sum(long long number)
{
	long long result = number;
	printf("Number :  %lld ", number);
	printf("\nFactors : [");
	for (int i = 1; i <= number / 2; ++i)
	{
		//Check whether resultant remainder of number divisible by n  is zero or not
		if (number % i == 0)
		{
			printf("%d,", i);
			//Sum Factors
			result += i;
		}
	}
	//Given number is itself factor
	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(1);
	return 0;
}

Output

Number :  50
Factors : [1,2,5,10,25,50]
Factors sum : 93

Number :  180
Factors : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum : 546

Number :  11
Factors : [1,11]
Factors sum : 12

Number :  1
Factors : [1]
Factors sum : 1
/*
  C++ program
  Find the sum of factors of a given number
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public: void factor_sum(long number)
	{
		long result = number;
		cout << "Number : " << number << " ";
		cout << "\nFactors : [";
		for (int i = 1; i <= number / 2; ++i)
		{
			//Check whether resultant remainder of number divisible by n  is zero or not
			if (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(1);
	return 0;
}

Output

Number : 50
Factors : [1,2,5,10,25,50]
Factors sum : 93

Number : 180
Factors : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum : 546

Number : 11
Factors : [1,11]
Factors sum : 12

Number : 1
Factors : [1]
Factors sum : 1
/*
  Java program
  Find the sum of factors of a given number
*/
public class MyNumber
{
	public void factor_sum(long number)
	{
		long result = number;
		System.out.print("Number : " + number + " ");
		System.out.print("\nFactors : [");
		for (int i = 1; i <= number / 2; ++i)
		{
			//Check whether resultant remainder of number divisible by n  is zero or not
			if (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(1);
	}
}

Output

Number : 50
Factors : [1,2,5,10,25,50]
Factors sum : 93

Number : 180
Factors : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum : 546

Number : 11
Factors : [1,11]
Factors sum : 12

Number : 1
Factors : [1]
Factors sum : 1
/*
  C# program
  Find the sum of factors of a given number
*/
using System;
public class MyNumber
{
	public void factor_sum(long number)
	{
		long result = number;
		Console.Write("Number : " + number + " ");
		Console.Write("\nFactors : [");
		for (int i = 1; i <= number / 2; i++)
		{
			//Check whether resultant reMainder of number divisible by n  is zero or not
			if (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(1);
	}
}

Output

Number : 50
Factors : [1,2,5,10,25,50]
Factors sum : 93

Number : 180
Factors : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum : 546

Number : 11
Factors : [1,11]
Factors sum : 12

Number : 1
Factors : [1]
Factors sum : 1
<?php
/*
  Php program
  Find the sum of factors of a given number
*/
class MyNumber
{
	public 	function factor_sum($number)
	{
		$result = $number;
		echo("Number : ". $number ." ");
		echo("\nFactors : [");
		for ($i = 1; $i <= intval($number / 2); ++$i)
		{
			//Check whether resultant remainder of number divisible by n  is zero or not
			if ($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(1);
}
main();

Output

Number : 50
Factors : [1,2,5,10,25,50]
Factors sum : 93

Number : 180
Factors : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum : 546

Number : 11
Factors : [1,11]
Factors sum : 12

Number : 1
Factors : [1]
Factors sum : 1
/*
  Node Js program
  Find the sum of factors of a given number
*/
class MyNumber
{
	factor_sum(number)
	{
		var result = number;
		process.stdout.write("Number : " + number + " ");
		process.stdout.write("\nFactors : [");
		for (var i = 1; i <= parseInt(number / 2); ++i)
		{
			//Check whether resultant remainder of number divisible by n  is zero or not
			if (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(1);
}
main();

Output

Number : 50
Factors : [1,2,5,10,25,50]
Factors sum : 93

Number : 180
Factors : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum : 546

Number : 11
Factors : [1,11]
Factors sum : 12

Number : 1
Factors : [1]
Factors sum : 1
#   Python 3 program
#   Find the sum of factors of a given number

class MyNumber :
	def factor_sum(self, number) :
		result = number
		print("Number : ", number ," ", end = "")
		print("\nFactors : [", end = "")
		i = 1
		while (i <= int(number / 2)) :
			# Check whether resultant remainder of number divisible by n  is zero or not
			if (number % i == 0) :
				print(i ,",", end = "")
				# Sum Factors
				result += i
			
			i += 1
		
		
		# Given number is itself factor
		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(1)


if __name__ == "__main__": main()

Output

Number :  50
Factors : [1 ,2 ,5 ,10 ,25 ,50]
Factors sum :  93

Number :  180
Factors : [1 ,2 ,3 ,4 ,5 ,6 ,9 ,10 ,12 ,15 ,18 ,20 ,30 ,36 ,45 ,60 ,90 ,180]
Factors sum :  546

Number :  11
Factors : [1 ,11]
Factors sum :  12

Number :  1
Factors : [1]
Factors sum :  1
#   Ruby program
#   Find the sum of factors of a given number

class MyNumber

	def factor_sum(number)
	
		result = number
		print("Number  : ", number ," ")
		print("\nFactors  : [")
		i = 1
		while (i <= number / 2)
		
			# Check whether resultant remainder of number divisible by n  is zero or not
			if (number % i == 0)
			
				print(i ,",")
				# Sum Factors
				result += i
			end
			i += 1
		end
		# Given number is itself factor
		print(number)
		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(1)
end
main()

Output

Number  : 50 
Factors  : [1,2,5,10,25,50]
Factors sum  : 93

Number  : 180 
Factors  : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum  : 546

Number  : 11 
Factors  : [1,11]
Factors sum  : 12

Number  : 1 
Factors  : [1]
Factors sum  : 1

/*
  Scala program
  Find the sum of factors of a given number
*/
class MyNumber
{
	def factor_sum(number: Long): Unit = {
		var result: Long = number;
		print("Number : " + number + " ");
		print("\nFactors : [");
		var i: Int = 1;
		while (i <= (number / 2).toInt)
		{
			//Check whether resultant remainder of number divisible by n  is zero or not
			if (number % i == 0)
			{
				print("" + i + ",");
				//Sum Factors
				result += i;
			}
			i += 1;
		}
		//Given number is itself factor
		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(1);
	}
}

Output

Number : 50
Factors : [1,2,5,10,25,50]
Factors sum : 93

Number : 180
Factors : [1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180]
Factors sum : 546

Number : 11
Factors : [1,11]
Factors sum : 12

Number : 1
Factors : [1]
Factors sum : 1
/*
  Swift program
  Find the sum of factors of a given number
*/
class MyNumber
{
	func factor_sum(_ number: Int)
	{
		var result: Int = number;
		print("Number : ", number ," ", terminator: "");
		print("\nFactors : [", terminator: "");
		var i: Int = 1;
		while (i <= number / 2)
		{
			//Check whether resultant remainder of number divisible by n  is zero or not
			if (number % i == 0)
			{
				print(i ,",", terminator: "");
				//Sum Factors
				result += i;
			}
			i += 1;
		}
		
		//Given number is itself factor
		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(1);
}
main();

Output

Number :  50
Factors : [1 ,2 ,5 ,10 ,25 ,50]
Factors sum :  93

Number :  180
Factors : [1 ,2 ,3 ,4 ,5 ,6 ,9 ,10 ,12 ,15 ,18 ,20 ,30 ,36 ,45 ,60 ,90 ,180]
Factors sum :  546

Number :  11
Factors : [1 ,11]
Factors sum :  12

Number :  1
Factors : [1]
Factors sum :  1


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