Find the factors of a number

Here given code implementation process.

//C Program
//Find the factor of a number
#include <stdio.h>

void find_factor(int number)
{
	printf("\nNumber [%d] : { ", number);
	int auxiliary = 1;
	while (auxiliary <= number / 2)
	{
		//Compare the given number, is divisible by auxiliary number or not
		if (number % auxiliary == 0)
		{
			printf("%d,", auxiliary);
		}
		auxiliary++;
	}
	printf("%d }\n", number);
}
int main()
{
	//Test Case 
	find_factor(145);
	find_factor(12);
	find_factor(2020);
	find_factor(11532);
	return 0;
}

Output

Number [145] : { 1,5,29,145 }

Number [12] : { 1,2,3,4,6,12 }

Number [2020] : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532] : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
/*
  C++ Program
  Find the factor of a number
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public: void find_factor(int number)
	{
		cout << "\nNumber [" << number << "] : { ";
		int auxiliary = 1;
		while (auxiliary <= number / 2)
		{
			//Compare the given number, is divisible by auxiliary number or not
			if (number % auxiliary == 0)
			{
				cout << "" << auxiliary << ",";
			}
			auxiliary++;
		}
		cout << "" << number << " }\n";
	}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Case 
	obj.find_factor(145);
	obj.find_factor(12);
	obj.find_factor(2020);
	obj.find_factor(11532);
	return 0;
}

Output

Number [145] : { 1,5,29,145 }

Number [12] : { 1,2,3,4,6,12 }

Number [2020] : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532] : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
/*
  Java Program
  Find the factor of a number
*/
public class MyNumber
{
  public void find_factor(int number)
  {
    System.out.print("\nNumber ["+number+"] : { ");
    int auxiliary = 1;
    while (auxiliary <= number / 2)
    {
      //Compare the given number, is divisible by auxiliary number or not
      if (number % auxiliary == 0)
      {
        System.out.print(""+auxiliary+",");
      }
      auxiliary++;
    }
    System.out.print(""+number+" }\n" );
  }
  
  public static void main(String[] args)
  {
    MyNumber obj = new MyNumber();
    //Test Case 
    obj.find_factor(145);
    obj.find_factor(12);
    obj.find_factor(2020);
    obj.find_factor(11532);
  }
}

Output

Number [145] : { 1,5,29,145 }

Number [12] : { 1,2,3,4,6,12 }

Number [2020] : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532] : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
/*
  C# Program
  Find the factor of a number
*/
using System;
public class MyNumber
{
	public void find_factor(int number)
	{
		Console.Write("\nNumber [" + number + "] : { ");
		int auxiliary = 1;
		while (auxiliary <= number / 2)
		{
			//Compare the given number, is divisible by auxiliary number or not
			if (number % auxiliary == 0)
			{
				Console.Write("" + auxiliary + ",");
			}
			auxiliary++;
		}
		Console.Write("" + number + " }\n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case 
		obj.find_factor(145);
		obj.find_factor(12);
		obj.find_factor(2020);
		obj.find_factor(11532);
	}
}

Output

Number [145] : { 1,5,29,145 }

Number [12] : { 1,2,3,4,6,12 }

Number [2020] : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532] : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
<?php
/*
  Php Program
  Find the factor of a number
*/
class MyNumber
{
	public 	function find_factor($number)
	{
		echo("\nNumber [". $number ."] : { ");
		$auxiliary = 1;
		while ($auxiliary <= intval($number / 2))
		{
			//Compare the given number, is divisible by auxiliary number or not
			if ($number % $auxiliary == 0)
			{
				echo("". $auxiliary .",");
			}
			$auxiliary++;
		}
		echo("". $number ." }\n");
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Case 
	$obj->find_factor(145);
	$obj->find_factor(12);
	$obj->find_factor(2020);
	$obj->find_factor(11532);
}
main();

Output

Number [145] : { 1,5,29,145 }

Number [12] : { 1,2,3,4,6,12 }

Number [2020] : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532] : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
/*
  Node Js Program
  Find the factor of a number
*/
class MyNumber
{
	find_factor(number)
	{
		process.stdout.write("\nNumber [" + number + "] : { ");
		var auxiliary = 1;
		while (auxiliary <= parseInt(number / 2))
		{
			//Compare the given number, is divisible by auxiliary number or not
			if (number % auxiliary == 0)
			{
				process.stdout.write("" + auxiliary + ",");
			}
			auxiliary++;
		}
		process.stdout.write("" + number + " }\n");
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Case 
	obj.find_factor(145);
	obj.find_factor(12);
	obj.find_factor(2020);
	obj.find_factor(11532);
}
main();

Output

Number [145] : { 1,5,29,145 }

Number [12] : { 1,2,3,4,6,12 }

Number [2020] : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532] : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
#   Python 3 Program
#   Find the factor of a number

class MyNumber :
	def find_factor(self, number) :
		print("\nNumber [", number ,"] : { ", end = "")
		auxiliary = 1
		while (auxiliary <= int(number / 2)) :
			# Compare the given number, is divisible by auxiliary number or not
			if (number % auxiliary == 0) :
				print( auxiliary ,",", end = "")
			
			auxiliary += 1
		
		print("", number,end=" }" )
	

def main() :
	obj = MyNumber()
	# Test Case 
	obj.find_factor(145)
	obj.find_factor(12)
	obj.find_factor(2020)
	obj.find_factor(11532)


if __name__ == "__main__": main()

Output

Number [ 145 ] : { 1 ,5 ,29 , 145 }
Number [ 12 ] : { 1 ,2 ,3 ,4 ,6 , 12 }
Number [ 2020 ] : { 1 ,2 ,4 ,5 ,10 ,20 ,101 ,202 ,404 ,505 ,1010 , 2020 }
Number [ 11532 ] : { 1 ,2 ,3 ,4 ,6 ,12 ,31 ,62 ,93 ,124 ,186 ,372 ,961 ,1922 ,2883 ,3844 ,5766 , 11532 }
#   Ruby Program
#   Find the factor of a number

class MyNumber

	def find_factor(number)
	
		print("\nNumber [", number ,"]  : { ")
		auxiliary = 1
		while (auxiliary <= number / 2)
		
			# Compare the given number, is divisible by auxiliary number or not
			if (number % auxiliary == 0)
			
				print("", auxiliary ,",")
			end
			auxiliary += 1
		end
		print("", number ," }\n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Case 
	obj.find_factor(145)
	obj.find_factor(12)
	obj.find_factor(2020)
	obj.find_factor(11532)
end
main()

Output

Number [145]  : { 1,5,29,145 }

Number [12]  : { 1,2,3,4,6,12 }

Number [2020]  : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532]  : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
/*
  Scala Program
  Find the factor of a number
*/
class MyNumber
{
	def find_factor(number: Int): Unit = {
		print("\nNumber [" + number + "] : { ");
		var auxiliary: Int = 1;
		while (auxiliary <= (number / 2).toInt)
		{
			//Compare the given number, is divisible by auxiliary number or not
			if (number % auxiliary == 0)
			{
				print("" + auxiliary + ",");
			}
			auxiliary += 1;
		}
		print("" + number + " }\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case 
		obj.find_factor(145);
		obj.find_factor(12);
		obj.find_factor(2020);
		obj.find_factor(11532);
	}
}

Output

Number [145] : { 1,5,29,145 }

Number [12] : { 1,2,3,4,6,12 }

Number [2020] : { 1,2,4,5,10,20,101,202,404,505,1010,2020 }

Number [11532] : { 1,2,3,4,6,12,31,62,93,124,186,372,961,1922,2883,3844,5766,11532 }
/*
  Swift Program
  Find the factor of a number
*/
class MyNumber
{
	func find_factor(_ number: Int)
	{
		print("\nNumber [", number ,"] : { ", terminator: "");
		var auxiliary: Int = 1;
		while (auxiliary <= number / 2)
		{
			//Compare the given number, is divisible by auxiliary number or not
			if (number % auxiliary == 0)
			{
				print("", auxiliary ,",", terminator: "");
			}
			auxiliary += 1;
		}
		print( number ,"}\n", terminator: "");
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Case 
	obj.find_factor(145);
	obj.find_factor(12);
	obj.find_factor(2020);
	obj.find_factor(11532);
}
main();

Output

Number [ 145 ] : {  1 , 5 , 29 ,145 }

Number [ 12 ] : {  1 , 2 , 3 , 4 , 6 ,12 }

Number [ 2020 ] : {  1 , 2 , 4 , 5 , 10 , 20 , 101 , 202 , 404 , 505 , 1010 ,2020 }

Number [ 11532 ] : {  1 , 2 , 3 , 4 , 6 , 12 , 31 , 62 , 93 , 124 , 186 , 372 , 961 , 1922 , 2883 , 3844 , 5766 ,11532 }


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