Number system conversion

Here given code implementation process.

// Java program
// Number system conversion
class NumberConversion
{
	public char actual_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	// Convert number from one base to another
	// Source number is form of decimal value, and source base are indicate its actual base.
	public void one_to_other(int source_number, int source_base, int destination_base)
	{
		System.out.print("\n Source Number    : " + source_number);
		System.out.print("\n Source Base      : " + source_base);
		System.out.print("\n Destination Base : " + destination_base);
		//Define some auxiliary variables
		int multiplier = 1;
		int decimal = 0;
		if (source_base != 10)
		{
			//When source number is not decimal then get decimal
			while (source_number != 0)
			{
				decimal += (source_number % 10) * multiplier;
				multiplier *= source_base;
				source_number /= 10;
			}
		}
		else
		{
			decimal = source_number;
		}
		//This is used to store result
		String result = "";
		while (decimal > 0)
		{
			result = (actual_value(decimal % destination_base)) + result;
			decimal /= destination_base;
		}
		//Display result
		System.out.print("\n Result : " + result + "\n");
	}
	public static void main(String[] args) throws Exception
	{
		NumberConversion obj = new NumberConversion();
		//Test Case
		//Convert a decimal 16 to octal
		obj.one_to_other(16, 10, 8);
		//Convert a octal 21 to decimal
		obj.one_to_other(21, 8, 10);
		//Convert a decimal 122 to hexa
		obj.one_to_other(122, 10, 16);
		//Convert a octal 32 to binary
		obj.one_to_other(32, 8, 2);
		//Convert a hexa 20 to binary
		obj.one_to_other(20, 16, 2);
		//Convert a base 5 (32) to base 2
		obj.one_to_other(34, 5, 2);
	}
}

Output

 Source Number    : 16
 Source Base      : 10
 Destination Base : 8
 Result : 20

 Source Number    : 21
 Source Base      : 8
 Destination Base : 10
 Result : 17

 Source Number    : 122
 Source Base      : 10
 Destination Base : 16
 Result : 7A

 Source Number    : 32
 Source Base      : 8
 Destination Base : 2
 Result : 11010

 Source Number    : 20
 Source Base      : 16
 Destination Base : 2
 Result : 100000

 Source Number    : 34
 Source Base      : 5
 Destination Base : 2
 Result : 10011
//Include header file
#include <iostream>
using namespace std;

// C++ program
// Number system conversion

class NumberConversion
{
	public: char actual_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	// Convert number from one base to another
	// Source number is form of decimal value, and source base are indicate its actual base.
	void one_to_other(int source_number, int source_base, int destination_base)
	{
		cout << "\n Source Number    : " << source_number;
		cout << "\n Source Base      : " << source_base;
		cout << "\n Destination Base : " << destination_base;
		//Define some auxiliary variables
		int multiplier = 1;
		int decimal = 0;
		if (source_base != 10)
		{
			//When source number is not decimal then get decimal
			while (source_number != 0)
			{
				decimal += (source_number % 10) * multiplier;
				multiplier *= source_base;
				source_number /= 10;
			}
		}
		else
		{
			decimal = source_number;
		}
		//This is used to store result
		string result = "";
		while (decimal > 0)
		{
			result = (this->actual_value(decimal % destination_base)) + result;
			decimal /= destination_base;
		}
		//Display result
		cout << "\n Result : " << result << "\n";
	}
};
int main()
{
	NumberConversion obj = NumberConversion();
	//Test Case
	//Convert a decimal 16 to octal
	obj.one_to_other(16, 10, 8);
	//Convert a octal 21 to decimal
	obj.one_to_other(21, 8, 10);
	//Convert a decimal 122 to hexa
	obj.one_to_other(122, 10, 16);
	//Convert a octal 32 to binary
	obj.one_to_other(32, 8, 2);
	//Convert a hexa 20 to binary
	obj.one_to_other(20, 16, 2);
	//Convert a base 5 (32) to base 2
	obj.one_to_other(34, 5, 2);
	return 0;
}

Output

 Source Number    : 16
 Source Base      : 10
 Destination Base : 8
 Result : 20

 Source Number    : 21
 Source Base      : 8
 Destination Base : 10
 Result : 17

 Source Number    : 122
 Source Base      : 10
 Destination Base : 16
 Result : 7A

 Source Number    : 32
 Source Base      : 8
 Destination Base : 2
 Result : 11010

 Source Number    : 20
 Source Base      : 16
 Destination Base : 2
 Result : 100000

 Source Number    : 34
 Source Base      : 5
 Destination Base : 2
 Result : 10011
//Include namespace system
using System;
// C# program
// Number system conversion
class NumberConversion
{
	public char actual_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	// Convert number from one base to another
	// Source number is form of decimal value, and source base are indicate its actual base.
	public void one_to_other(int source_number, int source_base, int destination_base)
	{
		Console.Write("\n Source Number    : " + source_number);
		Console.Write("\n Source Base      : " + source_base);
		Console.Write("\n Destination Base : " + destination_base);
		//Define some auxiliary variables
		int multiplier = 1;
		int decimal_no = 0;
		if (source_base != 10)
		{
			//When source number is not decimal then get decimal
			while (source_number != 0)
			{
				decimal_no += (source_number % 10) * multiplier;
				multiplier *= source_base;
				source_number /= 10;
			}
		}
		else
		{
			decimal_no = source_number;
		}
		//This is used to store result
		String result = "";
		while (decimal_no > 0)
		{
			result = (actual_value(decimal_no % destination_base)) + result;
			decimal_no /= destination_base;
		}
		//Display result
		Console.Write("\n Result : " + result + "\n");
	}
	public static void Main(String[] args) 
	{
		NumberConversion obj = new NumberConversion();
		//Test Case
		//Convert a decimal 16 to octal
		obj.one_to_other(16, 10, 8);
		//Convert a octal 21 to decimal
		obj.one_to_other(21, 8, 10);
		//Convert a decimal 122 to hexa
		obj.one_to_other(122, 10, 16);
		//Convert a octal 32 to binary
		obj.one_to_other(32, 8, 2);
		//Convert a hexa 20 to binary
		obj.one_to_other(20, 16, 2);
		//Convert a base 5 (32) to base 2
		obj.one_to_other(34, 5, 2);
	}
}

Output

 Source Number    : 16
 Source Base      : 10
 Destination Base : 8
 Result : 20

 Source Number    : 21
 Source Base      : 8
 Destination Base : 10
 Result : 17

 Source Number    : 122
 Source Base      : 10
 Destination Base : 16
 Result : 7A

 Source Number    : 32
 Source Base      : 8
 Destination Base : 2
 Result : 11010

 Source Number    : 20
 Source Base      : 16
 Destination Base : 2
 Result : 100000

 Source Number    : 34
 Source Base      : 5
 Destination Base : 2
 Result : 10011
<?php
// Php program
// Number system conversion
class NumberConversion
{
	//Get valid value
	public	function actual_value($num)
	{
		if ($num >= 0 && $num <= 9)
		{
			return (chr($num + ord('0')));
		}
		else
		{
			return (chr($num - 10 +  ord('A')));
		}
	}
	// Convert number from one base to another
	// Source number is form of decimal value, and source base are indicate its actual base.
	public	function one_to_other($source_number, $source_base, $destination_base)
	{
		echo "\n Source Number    : ". $source_number;
		echo "\n Source Base      : ". $source_base;
		echo "\n Destination Base : ". $destination_base;
		//Define some auxiliary variables
		$multiplier = 1;
		$decimal = 0;
		if ($source_base != 10)
		{
			//When source number is not decimal then get decimal
			while ($source_number != 0)
			{
				$decimal += ($source_number % 10) * $multiplier;
				$multiplier *= $source_base;
				$source_number = intval($source_number / 10);
			}
		}
		else
		{
			$decimal = $source_number;
		}
		//This is used to store result
		$result = "";
		while ($decimal > 0)
		{
			$result = ($this->actual_value($decimal % $destination_base)) . $result;
			$decimal = intval($decimal / $destination_base);
		}
		//Display result
		echo "\n Result : ". $result ."\n";
	}
}

function main()
{
	$obj = new NumberConversion();
	//Test Case
	//Convert a decimal 16 to octal
	$obj->one_to_other(16, 10, 8);
	//Convert a octal 21 to decimal
	$obj->one_to_other(21, 8, 10);
	//Convert a decimal 122 to hexa
	$obj->one_to_other(122, 10, 16);
	//Convert a octal 32 to binary
	$obj->one_to_other(32, 8, 2);
	//Convert a hexa 20 to binary
	$obj->one_to_other(20, 16, 2);
	//Convert a base 5 (32) to base 2
	$obj->one_to_other(34, 5, 2);
}
main();

Output

 Source Number    : 16
 Source Base      : 10
 Destination Base : 8
 Result : 20

 Source Number    : 21
 Source Base      : 8
 Destination Base : 10
 Result : 17

 Source Number    : 122
 Source Base      : 10
 Destination Base : 16
 Result : 7A

 Source Number    : 32
 Source Base      : 8
 Destination Base : 2
 Result : 11010

 Source Number    : 20
 Source Base      : 16
 Destination Base : 2
 Result : 100000

 Source Number    : 34
 Source Base      : 5
 Destination Base : 2
 Result : 10011
// Node Js program
// Number system conversion
class NumberConversion
{
	//Get valid value
	actual_value(num)
	{
		if (num >= 0 && num <= 9)
		{
			return (String.fromCharCode(num + '0'.charCodeAt(0)));
		}
		else
		{
			return (String.fromCharCode(num - 10 + 'A'.charCodeAt(0)));
		}
	}
	// Convert number from one base to another
	// Source number is form of decimal value, and source base are indicate its actual base.
	one_to_other(source_number, source_base, destination_base)
	{
		process.stdout.write("\n Source Number    : " + source_number);
		process.stdout.write("\n Source Base      : " + source_base);
		process.stdout.write("\n Destination Base : " + destination_base);
		//Define some auxiliary variables
		var multiplier = 1;
		var decimal = 0;
		if (source_base != 10)
		{
			//When source number is not decimal then get decimal
			while (source_number != 0)
			{
				decimal += (source_number % 10) * multiplier;
				multiplier *= source_base;
				source_number = parseInt(source_number / 10);
			}
		}
		else
		{
			decimal = source_number;
		}
		//This is used to store result
		var result = "";
		while (decimal > 0)
		{
			result = (this.actual_value(decimal % destination_base)) + result;
			decimal = parseInt(decimal / destination_base);
		}
		//Display result
		process.stdout.write("\n Result : " + result + "\n");
	}
}

function main()
{
	var obj = new NumberConversion();
	//Test Case
	//Convert a decimal 16 to octal
	obj.one_to_other(16, 10, 8);
	//Convert a octal 21 to decimal
	obj.one_to_other(21, 8, 10);
	//Convert a decimal 122 to hexa
	obj.one_to_other(122, 10, 16);
	//Convert a octal 32 to binary
	obj.one_to_other(32, 8, 2);
	//Convert a hexa 20 to binary
	obj.one_to_other(20, 16, 2);
	//Convert a base 5 (32) to base 2
	obj.one_to_other(34, 5, 2);
}
main();

Output

 Source Number    : 16
 Source Base      : 10
 Destination Base : 8
 Result : 20

 Source Number    : 21
 Source Base      : 8
 Destination Base : 10
 Result : 17

 Source Number    : 122
 Source Base      : 10
 Destination Base : 16
 Result : 7A

 Source Number    : 32
 Source Base      : 8
 Destination Base : 2
 Result : 11010

 Source Number    : 20
 Source Base      : 16
 Destination Base : 2
 Result : 100000

 Source Number    : 34
 Source Base      : 5
 Destination Base : 2
 Result : 10011
#  Python 3 program
#  Number system conversion
class NumberConversion :
	# Get valid value
	def actual_value(self, num) :
		if (num >= 0 and num <= 9) :
			return (chr(num + ord('0')))
		else :
			return (chr(num - 10 + ord('A')))
		
	#  Convert number from one base to another
	#  Source number is form of decimal value, and source base are indicate its actual base.
	def one_to_other(self, source_number, source_base, destination_base) :
		print("\n Source Number    : ", source_number, end = "")
		print("\n Source Base      : ", source_base, end = "")
		print("\n Destination Base : ", destination_base, end = "")
		# Define some auxiliary variables
		multiplier = 1
		decimal = 0
		if (source_base != 10) :
			# When source number is not decimal then get decimal
			while (source_number != 0) :
				decimal += (source_number % 10) * multiplier
				multiplier *= source_base
				source_number = int(source_number / 10)
			
		else :
			decimal = source_number
		
		# This is used to store result
		result = ""
		while (decimal > 0) :
			result = (self.actual_value(decimal % destination_base)) + result
			decimal = int(decimal / destination_base)
		
		# Display result
		print("\n Result : ", result ,"\n", end = "")
	

def main() :
	obj = NumberConversion()
	# Test Case
	# Convert a decimal 16 to octal
	obj.one_to_other(16, 10, 8)
	# Convert a octal 21 to decimal
	obj.one_to_other(21, 8, 10)
	# Convert a decimal 122 to hexa
	obj.one_to_other(122, 10, 16)
	# Convert a octal 32 to binary
	obj.one_to_other(32, 8, 2)
	# Convert a hexa 20 to binary
	obj.one_to_other(20, 16, 2)
	# Convert a base 5 (32) to base 2
	obj.one_to_other(34, 5, 2)

if __name__ == "__main__": main()

Output

 Source Number    :  16
 Source Base      :  10
 Destination Base :  8
 Result :  20

 Source Number    :  21
 Source Base      :  8
 Destination Base :  10
 Result :  17

 Source Number    :  122
 Source Base      :  10
 Destination Base :  16
 Result :  7A

 Source Number    :  32
 Source Base      :  8
 Destination Base :  2
 Result :  11010

 Source Number    :  20
 Source Base      :  16
 Destination Base :  2
 Result :  100000

 Source Number    :  34
 Source Base      :  5
 Destination Base :  2
 Result :  10011
// Scala program
// Number system conversion
class NumberConversion
{
	//Get valid value
	def actual_value(num: Int): Char = {
		if (num >= 0 && num <= 9)
		{
			return (num + '0').toChar;
		}
		else
		{
			return (num - 10 + 'A').toChar;
		}
	}
	// Convert number from one base to another
	// Source number is form of decimal value, and source base are indicate its actual base.
	def one_to_other(num: Int, source_base: Int, destination_base: Int): Unit = {
		var source_number = num;
        print("\n Source Number    : " + source_number);
		print("\n Source Base      : " + source_base);
		print("\n Destination Base : " + destination_base);
		//Define some auxiliary variables
		var multiplier: Int = 1;
		var decimal: Int = 0;
		if (source_base != 10)
		{
			//When source number is not decimal then get decimal
			while (source_number != 0)
			{
				decimal += (source_number % 10) * multiplier;
				multiplier *= source_base;
				source_number = (source_number / 10).toInt;
			}
		}
		else
		{
			decimal = source_number;
		}
		//This is used to store result
		var result: String = "";
		while (decimal > 0)
		{
			result = ""+(actual_value(decimal % destination_base)) + result;
			decimal = (decimal / destination_base).toInt;
		}
		//Display result
		print("\n Result : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: NumberConversion = new NumberConversion();
		//Test Case
		//Convert a decimal 16 to octal
		obj.one_to_other(16, 10, 8);
		//Convert a octal 21 to decimal
		obj.one_to_other(21, 8, 10);
		//Convert a decimal 122 to hexa
		obj.one_to_other(122, 10, 16);
		//Convert a octal 32 to binary
		obj.one_to_other(32, 8, 2);
		//Convert a hexa 20 to binary
		obj.one_to_other(20, 16, 2);
		//Convert a base 5 (32) to base 2
		obj.one_to_other(34, 5, 2);
	}
}

Output

 Source Number    : 16
 Source Base      : 10
 Destination Base : 8
 Result : 20

 Source Number    : 21
 Source Base      : 8
 Destination Base : 10
 Result : 17

 Source Number    : 122
 Source Base      : 10
 Destination Base : 16
 Result : 7A

 Source Number    : 32
 Source Base      : 8
 Destination Base : 2
 Result : 11010

 Source Number    : 20
 Source Base      : 16
 Destination Base : 2
 Result : 100000

 Source Number    : 34
 Source Base      : 5
 Destination Base : 2
 Result : 10011
#  Ruby program
#  Number system conversion
class NumberConversion

	# Get valid value
	def actual_value(num)
	
		if (num >= 0 && num <= 9)
		
			return ((num + ('0'.ord)).chr).to_s
		else
		
			return ((num - 10 + ('A'.ord)).chr).to_s
		end
	end
	#  Convert number from one base to another
	#  Source number is form of decimal value, and source base are indicate its actual base.
	def one_to_other(source_number, source_base, destination_base)
	
		print("\n Source Number    : ", source_number)
		print("\n Source Base      : ", source_base)
		print("\n Destination Base : ", destination_base)
		# Define some auxiliary variables
		multiplier = 1
		decimal = 0
		if (source_base != 10)
		
			# When source number is not decimal then get decimal
			while (source_number != 0)
			
				decimal += (source_number % 10) * multiplier
				multiplier *= source_base
				source_number = source_number / 10
			end
		else
		
			decimal = source_number
		end
		# This is used to store result
		result = ""
		while (decimal > 0)
		
			result = (self.actual_value(decimal % destination_base)) + result
			decimal = decimal / destination_base
		end
		# Display result
		print("\n Result : ", result ,"\n")
	end
end
def main()

	obj = NumberConversion.new()
	# Test Case
	# Convert a decimal 16 to octal
	obj.one_to_other(16, 10, 8)
	# Convert a octal 21 to decimal
	obj.one_to_other(21, 8, 10)
	# Convert a decimal 122 to hexa
	obj.one_to_other(122, 10, 16)
	# Convert a octal 32 to binary
	obj.one_to_other(32, 8, 2)
	# Convert a hexa 20 to binary
	obj.one_to_other(20, 16, 2)
	# Convert a base 5 (32) to base 2
	obj.one_to_other(34, 5, 2)
end
main()

Output

 Source Number    : 16
 Source Base      : 10
 Destination Base : 8
 Result : 20

 Source Number    : 21
 Source Base      : 8
 Destination Base : 10
 Result : 17

 Source Number    : 122
 Source Base      : 10
 Destination Base : 16
 Result : 7A

 Source Number    : 32
 Source Base      : 8
 Destination Base : 2
 Result : 11010

 Source Number    : 20
 Source Base      : 16
 Destination Base : 2
 Result : 100000

 Source Number    : 34
 Source Base      : 5
 Destination Base : 2
 Result : 10011
// Swift program
// Number system conversion
class NumberConversion
{
    //Get valid value
    func actual_value(_ num: Int) -> String
    {
        var result  = "";
        
        if (num >= 0 && num <= 9)
        {
            result=String(UnicodeScalar(UInt8( num + Int(UnicodeScalar("0")!.value ))));
        }
        else
        {
           
           result = String(UnicodeScalar(UInt8((num - 10) + Int(UnicodeScalar("A")!.value ))));
        }
        
         return result;
        
    }
	// Convert number from one base to another
	// Source number is form of decimal value, and source base are indicate its actual base.
	func one_to_other(_ num:  Int, _ source_base: Int, _ destination_base: Int)
	{
        var source_number : Int = num;
		print("\n Source Number    : ", source_number, terminator: "");
		print("\n Source Base      : ", source_base, terminator: "");
		print("\n Destination Base : ", destination_base, terminator: "");
		//Define some auxiliary variables
		var multiplier: Int = 1;
		var decimal: Int = 0;
		if (source_base != 10)
		{
			//When source number is not decimal then get decimal
			while (source_number != 0)
			{
				decimal += (source_number % 10) * multiplier;
				multiplier *= source_base;
				source_number = source_number / 10;
			}
		}
		else
		{
			decimal = source_number;
		}
		//This is used to store result
		var result: String = "";
		while (decimal > 0)
		{
			result = (self.actual_value(decimal % destination_base)) + result;
			decimal = decimal / destination_base;
		}
		//Display result
		print("\n Result : ", result );
	}
}
func main()
{
	let obj: NumberConversion = NumberConversion();
	//Test Case
	//Convert a decimal 16 to octal
	obj.one_to_other(16, 10, 8);
	//Convert a octal 21 to decimal
	obj.one_to_other(21, 8, 10);
	//Convert a decimal 122 to hexa
	obj.one_to_other(122, 10, 16);
	//Convert a octal 32 to binary
	obj.one_to_other(32, 8, 2);
	//Convert a hexa 20 to binary
	obj.one_to_other(20, 16, 2);
	//Convert a base 5 (32) to base 2
	obj.one_to_other(34, 5, 2);
}
main();

Output

 Source Number    :  16
 Source Base      :  10
 Destination Base :  8
 Result :  20

 Source Number    :  21
 Source Base      :  8
 Destination Base :  10
 Result :  17

 Source Number    :  122
 Source Base      :  10
 Destination Base :  16
 Result :  7A

 Source Number    :  32
 Source Base      :  8
 Destination Base :  2
 Result :  11010

 Source Number    :  20
 Source Base      :  16
 Destination Base :  2
 Result :  100000

 Source Number    :  34
 Source Base      :  5
 Destination Base :  2
 Result :  10011


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