Find sum of odd factors of a number

Here given code implementation process.

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

// Calculates Sum of odd factors of a number
void oddFactorSum(int num)
{
	printf("\n Odd Factors pair of a number %d is \n", num);
	int sum = 0;
	// Execute loop through by 1 to num/2
	for (int i = 1; i <= num / 2; ++i)
	{
		if (i % 2 != 0 && num % i == 0)
		{
			printf(" (%d X %d) \n", i, num / i);
			// Add odd factor
			sum += i;
		}
	}
	// Display calculated result
	printf(" Odd Factors Sum : %d\n", sum);
}
int main()
{
	// Test Cases
	oddFactorSum(12);
	oddFactorSum(50);
	oddFactorSum(40);
	return 0;
}

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6
/*
   Java Program
   Find sum of odd factors of a number
*/
public class Factorization
{
	// Calculates Sum of odd factors of a number
	public void oddFactorSum(int num)
	{
		System.out.print("\n Odd Factors pair of a number " + num + " is \n");
		int sum = 0;
		// Execute loop through by 1 to num/2
		for (int i = 1; i <= num / 2; ++i)
		{
			if (i % 2 != 0 && num % i == 0)
			{
				System.out.print(" (" + i + " X " + num / i + ") \n");
				// Add odd factor
				sum += i;
			}
		}
		// Display calculated result
		System.out.print(" Odd Factors Sum : " + sum + "\n");
	}
	public static void main(String[] args)
	{
		Factorization task = new Factorization();
		// Test Cases
		task.oddFactorSum(12);
		task.oddFactorSum(50);
		task.oddFactorSum(40);
	}
}

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6
// Include header file
#include <iostream>
using namespace std;

/*
   C++ Program
   Find sum of odd factors of a number
*/

class Factorization
{
	public:
		// Calculates Sum of odd factors of a number
		void oddFactorSum(int num)
		{
			cout << "\n Odd Factors pair of a number " << num << " is \n";
			int sum = 0;
			// Execute loop through by 1 to num/2
			for (int i = 1; i <= num / 2; ++i)
			{
				if (i % 2 != 0 && num % i == 0)
				{
					cout << " (" << i << " X " << num / i << ") \n";
					// Add odd factor
					sum += i;
				}
			}
			// Display calculated result
			cout << " Odd Factors Sum : " << sum << "\n";
		}
};
int main()
{
	Factorization task = Factorization();
	// Test Cases
	task.oddFactorSum(12);
	task.oddFactorSum(50);
	task.oddFactorSum(40);
	return 0;
}

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6
// Include namespace system
using System;
/*
   C# Program
   Find sum of odd factors of a number
*/
public class Factorization
{
	// Calculates Sum of odd factors of a number
	public void oddFactorSum(int num)
	{
		Console.Write("\n Odd Factors pair of a number " + num + " is \n");
		int sum = 0;
		// Execute loop through by 1 to num/2
		for (int i = 1; i <= num / 2; ++i)
		{
			if (i % 2 != 0 && num % i == 0)
			{
				Console.Write(" (" + i + " X " + num / i + ") \n");
				// Add odd factor
				sum += i;
			}
		}
		// Display calculated result
		Console.Write(" Odd Factors Sum : " + sum + "\n");
	}
	public static void Main(String[] args)
	{
		Factorization task = new Factorization();
		// Test Cases
		task.oddFactorSum(12);
		task.oddFactorSum(50);
		task.oddFactorSum(40);
	}
}

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6
<?php
/*
   Php Program
   Find sum of odd factors of a number
*/
class Factorization
{
	// Calculates Sum of odd factors of a number
	public	function oddFactorSum($num)
	{
		echo "\n Odd Factors pair of a number ". $num ." is \n";
		$sum = 0;
		// Execute loop through by 1 to num/2
		for ($i = 1; $i <= intval($num / 2); ++$i)
		{
			if ($i % 2 != 0 && $num % $i == 0)
			{
				echo " (". $i ." X ". intval($num / $i) .") \n";
				// Add odd factor
				$sum += $i;
			}
		}
		// Display calculated result
		echo " Odd Factors Sum : ". $sum ."\n";
	}
}

function main()
{
	$task = new Factorization();
	// Test Cases
	$task->oddFactorSum(12);
	$task->oddFactorSum(50);
	$task->oddFactorSum(40);
}
main();

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6
/*
   Node Js Program
   Find sum of odd factors of a number
*/
class Factorization
{
	// Calculates Sum of odd factors of a number
	oddFactorSum(num)
	{
		process.stdout.write("\n Odd Factors pair of a number " + num + " is \n");
		var sum = 0;
		// Execute loop through by 1 to num/2
		for (var i = 1; i <= parseInt(num / 2); ++i)
		{
			if (i % 2 != 0 && num % i == 0)
			{
				process.stdout.write(" (" + i + " X " + parseInt(num / i) + ") \n");
				// Add odd factor
				sum += i;
			}
		}
		// Display calculated result
		process.stdout.write(" Odd Factors Sum : " + sum + "\n");
	}
}

function main()
{
	var task = new Factorization();
	// Test Cases
	task.oddFactorSum(12);
	task.oddFactorSum(50);
	task.oddFactorSum(40);
}
main();

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6
#  Python 3 Program
#  Find sum of odd factors of a number

class Factorization :
	#  Calculates Sum of odd factors of a number
	def oddFactorSum(self, num) :
		print("\n Odd Factors pair of a number ", num ," is ")
		sum = 0
		i = 1
		#  Execute loop through by 1 to num/2
		while (i <= int(num / 2)) :
			if (i % 2 != 0 and num % i == 0) :
				print(" (", i ,"X", int(num / i) ,") ")
				#  Add odd factor
				sum += i
			
			i += 1
		
		#  Display calculated result
		print(" Odd Factors Sum : ", sum )
	

def main() :
	task = Factorization()
	#  Test Cases
	task.oddFactorSum(12)
	task.oddFactorSum(50)
	task.oddFactorSum(40)

if __name__ == "__main__": main()

Output

 Odd Factors pair of a number  12  is
 ( 1 X 12 )
 ( 3 X 4 )
 Odd Factors Sum :  4

 Odd Factors pair of a number  50  is
 ( 1 X 50 )
 ( 5 X 10 )
 ( 25 X 2 )
 Odd Factors Sum :  31

 Odd Factors pair of a number  40  is
 ( 1 X 40 )
 ( 5 X 8 )
 Odd Factors Sum :  6
#  Ruby Program
#  Find sum of odd factors of a number

class Factorization 
	#  Calculates Sum of odd factors of a number
	def oddFactorSum(num) 
		print("\n Odd Factors pair of a number ", num ," is \n")
		sum = 0
		i = 1
		#  Execute loop through by 1 to num/2
		while (i <= num / 2) 
			if (i % 2 != 0 && num % i == 0) 
				print(" (", i ," X ", num / i ,") \n")
				#  Add odd factor
				sum += i
			end

			i += 1
		end

		#  Display calculated result
		print(" Odd Factors Sum : ", sum ,"\n")
	end

end

def main() 
	task = Factorization.new()
	#  Test Cases
	task.oddFactorSum(12)
	task.oddFactorSum(50)
	task.oddFactorSum(40)
end

main()

Output

 Odd Factors pair of a number 12 is 
 (1 X 12) 
 (3 X 4) 
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is 
 (1 X 50) 
 (5 X 10) 
 (25 X 2) 
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is 
 (1 X 40) 
 (5 X 8) 
 Odd Factors Sum : 6
/*
   Scala Program
   Find sum of odd factors of a number
*/
class Factorization
{
	// Calculates Sum of odd factors of a number
	def oddFactorSum(num: Int): Unit = {
		print("\n Odd Factors pair of a number " + num + " is \n");
		var sum: Int = 0;
		var i: Int = 1;
		// Execute loop through by 1 to num/2
		while (i <= (num / 2).toInt)
		{
			if (i % 2 != 0 && num % i == 0)
			{
				print(" (" + i + " X " + (num / i).toInt + ") \n");
				// Add odd factor
				sum += i;
			}
			i += 1;
		}
		// Display calculated result
		print(" Odd Factors Sum : " + sum + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Factorization = new Factorization();
		// Test Cases
		task.oddFactorSum(12);
		task.oddFactorSum(50);
		task.oddFactorSum(40);
	}
}

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6
/*
   Swift 4 Program
   Find sum of odd factors of a number
*/
class Factorization
{
	// Calculates Sum of odd factors of a number
	func oddFactorSum(_ num: Int)
	{
		print("\n Odd Factors pair of a number ", num ," is ");
		var sum: Int = 0;
		var i: Int = 1;
		// Execute loop through by 1 to num/2
		while (i <= num / 2)
		{
			if (i % 2  != 0 && num % i == 0)
			{
				print(" (", i ," X ", num / i ,") ");
				// Add odd factor
				sum += i;
			}
			i += 1;
		}
		// Display calculated result
		print(" Odd Factors Sum : ", sum );
	}
}
func main()
{
	let task: Factorization = Factorization();
	// Test Cases
	task.oddFactorSum(12);
	task.oddFactorSum(50);
	task.oddFactorSum(40);
}
main();

Output

 Odd Factors pair of a number  12  is
 ( 1  X  12 )
 ( 3  X  4 )
 Odd Factors Sum :  4

 Odd Factors pair of a number  50  is
 ( 1  X  50 )
 ( 5  X  10 )
 ( 25  X  2 )
 Odd Factors Sum :  31

 Odd Factors pair of a number  40  is
 ( 1  X  40 )
 ( 5  X  8 )
 Odd Factors Sum :  6
/*
   Kotlin Program
   Find sum of odd factors of a number
*/
class Factorization
{
	// Calculates Sum of odd factors of a number
	fun oddFactorSum(num: Int): Unit
	{
		print("\n Odd Factors pair of a number " + num + " is \n");
		var sum: Int = 0;
		var i: Int = 1;
		// Execute loop through by 1 to num/2
		while (i <= num / 2)
		{
			if (i % 2 != 0 && num % i == 0)
			{
				print(" (" + i + " X " + num / i + ") \n");
				// Add odd factor
				sum += i;
			}
			i += 1;
		}
		// Display calculated result
		print(" Odd Factors Sum : " + sum + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Factorization = Factorization();
	// Test Cases
	task.oddFactorSum(12);
	task.oddFactorSum(50);
	task.oddFactorSum(40);
}

Output

 Odd Factors pair of a number 12 is
 (1 X 12)
 (3 X 4)
 Odd Factors Sum : 4

 Odd Factors pair of a number 50 is
 (1 X 50)
 (5 X 10)
 (25 X 2)
 Odd Factors Sum : 31

 Odd Factors pair of a number 40 is
 (1 X 40)
 (5 X 8)
 Odd Factors Sum : 6


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