Convert a number from decimal to other base

Here given code implementation process.

// Java program
// Convert a number from decimal to other base
class NumberConversion
{
	//Get valid value
	public char actual_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	//Convert decimal number in given base number
	public void decimal_to_other(int decimal_number, int destination_base)
	{
		System.out.print("\n Decimal Number   : " + decimal_number);
		System.out.print("\n Destination Base : " + destination_base);
		boolean flag = false;
		if (decimal_number < 0)
		{
			decimal_number = -decimal_number;
			flag = true;
		}
		//This is used to store result
		String result = "";
		//Transform decimal to other base
		while (decimal_number > 0)
		{
			result = (actual_value(decimal_number % destination_base)) + result;
			decimal_number /= destination_base;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		//Display result
		System.out.print("\n Result : " + result + "\n");
	}
	public static void main(String[] args) throws Exception
	{
		NumberConversion obj = new NumberConversion();
		//Test case
		obj.decimal_to_other(55, 2);
		obj.decimal_to_other(125, 16);
		obj.decimal_to_other(125, 8);
      	obj.decimal_to_other(-28, 16);
		obj.decimal_to_other(24, 4);
      	obj.decimal_to_other(2124, 16);
	}
}

Output

 Decimal Number   : 55
 Destination Base : 2
 Result : 110111

 Decimal Number   : 125
 Destination Base : 16
 Result : 7D

 Decimal Number   : 125
 Destination Base : 8
 Result : 175

 Decimal Number   : -28
 Destination Base : 16
 Result : -1C

 Decimal Number   : 24
 Destination Base : 4
 Result : 120

 Decimal Number   : 2124
 Destination Base : 16
 Result : 84C
//Include header file
#include <iostream>

using namespace std;
// C++ program
// Convert a number from decimal to other base
class NumberConversion
{
	public:
		//Get valid value
		char actual_value(int num)
		{
			if (num >= 0 && num <= 9)
			{
				return (char)(num + '0');
			}
			else
			{
				return (char)(num - 10 + 'A');
			}
		}
	//Convert decimal number in given base number
	void decimal_to_other(int decimal_number, int destination_base)
	{
		cout << "\n Decimal Number   : " << decimal_number;
		cout << "\n Destination Base : " << destination_base;
		bool flag = false;
		if (decimal_number < 0)
		{
			decimal_number = -decimal_number;
			flag = true;
		}
		//This is used to store result
		string result = "";
		//Transform decimal to other base
		while (decimal_number > 0)
		{
			result = (this->actual_value(decimal_number % destination_base)) + result;
			decimal_number /= destination_base;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		//Display result
		cout << "\n Result : " << result << "\n";
	}
};
int main()
{
	NumberConversion obj = NumberConversion();
	//Test case
	obj.decimal_to_other(55, 2);
	obj.decimal_to_other(125, 16);
	obj.decimal_to_other(125, 8);
	obj.decimal_to_other(-28, 16);
	obj.decimal_to_other(24, 4);
	obj.decimal_to_other(2124, 16);
	return 0;
}

Output

 Decimal Number   : 55
 Destination Base : 2
 Result : 110111

 Decimal Number   : 125
 Destination Base : 16
 Result : 7D

 Decimal Number   : 125
 Destination Base : 8
 Result : 175

 Decimal Number   : -28
 Destination Base : 16
 Result : -1C

 Decimal Number   : 24
 Destination Base : 4
 Result : 120

 Decimal Number   : 2124
 Destination Base : 16
 Result : 84C
//Include namespace system
using System;
// C# program
// Convert a number from decimal to other base
class NumberConversion
{
	//Get valid value
	public char actual_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	//Convert decimal number in given base number
	public void decimal_to_other(int decimal_number, int destination_base)
	{
		Console.Write("\n Decimal Number   : " + decimal_number);
		Console.Write("\n Destination Base : " + destination_base);
		Boolean flag = false;
		if (decimal_number < 0)
		{
			decimal_number = -decimal_number;
			flag = true;
		}
		//This is used to store result
		String result = "";
		//Transform decimal to other base
		while (decimal_number > 0)
		{
			result = (actual_value(decimal_number % destination_base)) + result;
			decimal_number /= destination_base;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		//Display result
		Console.Write("\n Result : " + result + "\n");
	}
	public static void Main(String[] args) 
	{
		NumberConversion obj = new NumberConversion();
		//Test case
		obj.decimal_to_other(55, 2);
		obj.decimal_to_other(125, 16);
		obj.decimal_to_other(125, 8);
		obj.decimal_to_other(-28, 16);
		obj.decimal_to_other(24, 4);
		obj.decimal_to_other(2124, 16);
	}
}

Output

 Decimal Number   : 55
 Destination Base : 2
 Result : 110111

 Decimal Number   : 125
 Destination Base : 16
 Result : 7D

 Decimal Number   : 125
 Destination Base : 8
 Result : 175

 Decimal Number   : -28
 Destination Base : 16
 Result : -1C

 Decimal Number   : 24
 Destination Base : 4
 Result : 120

 Decimal Number   : 2124
 Destination Base : 16
 Result : 84C
<?php
// Php program
// Convert a number from decimal to other base
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 decimal number in given base number
	public	function decimal_to_other($decimal_number, $destination_base)
	{
		echo "\n Decimal Number   : ". $decimal_number;
		echo "\n Destination Base : ". $destination_base;
		$flag = false;
		if ($decimal_number < 0)
		{
			$decimal_number = -$decimal_number;
			$flag = true;
		}
		//This is used to store result
		$result = "";
		//Transform decimal to other base
		while ($decimal_number > 0)
		{
			$result = ($this->actual_value($decimal_number % $destination_base)) . $result;
			$decimal_number = intval($decimal_number / $destination_base);
		}
		if ($flag == true)
		{
			$result = "-". $result;
		}
		//Display result
		echo "\n Result : ". $result ."\n";
	}
}

function main()
{
	$obj = new NumberConversion();
	//Test case
	$obj->decimal_to_other(55, 2);
	$obj->decimal_to_other(125, 16);
	$obj->decimal_to_other(125, 8);
	$obj->decimal_to_other(-28, 16);
	$obj->decimal_to_other(24, 4);
	$obj->decimal_to_other(2124, 16);
}
main();

Output

 Decimal Number   : 55
 Destination Base : 2
 Result : 110111

 Decimal Number   : 125
 Destination Base : 16
 Result : 7D

 Decimal Number   : 125
 Destination Base : 8
 Result : 175

 Decimal Number   : -28
 Destination Base : 16
 Result : -1C

 Decimal Number   : 24
 Destination Base : 4
 Result : 120

 Decimal Number   : 2124
 Destination Base : 16
 Result : 84C
// Node Js program
// Convert a number from decimal to other base
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 decimal number in given base number
	decimal_to_other(decimal_number, destination_base)
	{
		process.stdout.write("\n Decimal Number   : " + decimal_number);
		process.stdout.write("\n Destination Base : " + destination_base);
		var flag = false;
		if (decimal_number < 0)
		{
			decimal_number = -decimal_number;
			flag = true;
		}
		//This is used to store result
		var result = "";
		//Transform decimal to other base
		while (decimal_number > 0)
		{
			result = (this.actual_value(decimal_number % destination_base)) + result;
			decimal_number = parseInt(decimal_number / destination_base);
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		//Display result
		process.stdout.write("\n Result : " + result + "\n");
	}
}

function main()
{
	var obj = new NumberConversion();
	//Test case
	obj.decimal_to_other(55, 2);
	obj.decimal_to_other(125, 16);
	obj.decimal_to_other(125, 8);
	obj.decimal_to_other(-28, 16);
	obj.decimal_to_other(24, 4);
	obj.decimal_to_other(2124, 16);
}
main();

Output

 Decimal Number   : 55
 Destination Base : 2
 Result : 110111

 Decimal Number   : 125
 Destination Base : 16
 Result : 7D

 Decimal Number   : 125
 Destination Base : 8
 Result : 175

 Decimal Number   : -28
 Destination Base : 16
 Result : -1C

 Decimal Number   : 24
 Destination Base : 4
 Result : 120

 Decimal Number   : 2124
 Destination Base : 16
 Result : 84C
#  Python 3 program
#  Convert a number from decimal to other base
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 decimal number in given base number
	def decimal_to_other(self, decimal_number, destination_base) :
		print("\n Decimal Number   : ", decimal_number, end = "")
		print("\n Destination Base : ", destination_base, end = "")
		flag = False
		if (decimal_number < 0) :
			decimal_number = -decimal_number
			flag = True
		
		# This is used to store result
		result = ""
		# Transform decimal to other base
		while (decimal_number > 0) :
			result = (self.actual_value(decimal_number % destination_base)) + result
			decimal_number = int(decimal_number / destination_base)
		
		if (flag == True) :
			result = "-"+ result
		
		# Display result
		print("\n Result : ", result ,"\n", end = "")
	

def main() :
	obj = NumberConversion()
	# Test case
	obj.decimal_to_other(55, 2)
	obj.decimal_to_other(125, 16)
	obj.decimal_to_other(125, 8)
	obj.decimal_to_other(-28, 16)
	obj.decimal_to_other(24, 4)
	obj.decimal_to_other(2124, 16)

if __name__ == "__main__": main()

Output

 Decimal Number   :  55
 Destination Base :  2
 Result :  110111

 Decimal Number   :  125
 Destination Base :  16
 Result :  7D

 Decimal Number   :  125
 Destination Base :  8
 Result :  175

 Decimal Number   :  -28
 Destination Base :  16
 Result :  -1C

 Decimal Number   :  24
 Destination Base :  4
 Result :  120

 Decimal Number   :  2124
 Destination Base :  16
 Result :  84C
#  Ruby program
#  Convert a number from decimal to other base
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 decimal number in given base number
	def decimal_to_other(decimal_number, destination_base)
	
		print("\n Decimal Number   : ", decimal_number)
		print("\n Destination Base : ", destination_base)
		flag = false
		if (decimal_number < 0)
		
			decimal_number = -decimal_number
			flag = true
		end
		# This is used to store result
		result = ""
		# Transform decimal to other base
		while (decimal_number > 0)
		
			result = (self.actual_value(decimal_number % destination_base)) + result
			decimal_number = decimal_number / destination_base
		end
		if (flag == true)
		
			result = "-"+ result
		end
		# Display result
		print("\n Result : ", result ,"\n")
	end
end
def main()

	obj = NumberConversion.new()
	# Test case
	obj.decimal_to_other(55, 2)
	obj.decimal_to_other(125, 16)
	obj.decimal_to_other(125, 8)
	obj.decimal_to_other(-28, 16)
	obj.decimal_to_other(24, 4)
	obj.decimal_to_other(2124, 16)
end
main()

Output

 Decimal Number   : 55
 Destination Base : 2
 Result : 110111

 Decimal Number   : 125
 Destination Base : 16
 Result : 7D

 Decimal Number   : 125
 Destination Base : 8
 Result : 175

 Decimal Number   : -28
 Destination Base : 16
 Result : -1C

 Decimal Number   : 24
 Destination Base : 4
 Result : 120

 Decimal Number   : 2124
 Destination Base : 16
 Result : 84C
// Scala program
// Convert a number from decimal to other base
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 decimal number in given base number
	def decimal_to_other(decimal: Int, base: Int): Unit = {
      	var destination_base = base;
      	var decimal_number = decimal;
		print("\n Decimal Number   : " + decimal_number);
		print("\n Destination Base : " + destination_base);
		var flag: Boolean = false;
		if (decimal_number < 0)
		{
			decimal_number = -decimal_number;
			flag = true;
		}
		//This is used to store result
		var result: String = "";
		//Transform decimal to other base
		while (decimal_number > 0)
		{
			result = ""+(actual_value(decimal_number % destination_base)) + result;
			decimal_number = (decimal_number / destination_base).toInt;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		//Display result
		print("\n Result : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: NumberConversion = new NumberConversion();
		//Test case
		obj.decimal_to_other(55, 2);
		obj.decimal_to_other(125, 16);
		obj.decimal_to_other(125, 8);
		obj.decimal_to_other(-28, 16);
		obj.decimal_to_other(24, 4);
		obj.decimal_to_other(2124, 16);
	}
}

Output

 Decimal Number   : 55
 Destination Base : 2
 Result : 110111

 Decimal Number   : 125
 Destination Base : 16
 Result : 7D

 Decimal Number   : 125
 Destination Base : 8
 Result : 175

 Decimal Number   : -28
 Destination Base : 16
 Result : -1C

 Decimal Number   : 24
 Destination Base : 4
 Result : 120

 Decimal Number   : 2124
 Destination Base : 16
 Result : 84C
// Swift program
// Convert a number from decimal to other base
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 decimal number in given base number
    func decimal_to_other(_ decimal : Int, _ base : Int)
    {
        var decimal_number = decimal;
        let destination_base = base;
        print("\n Decimal Number   : ", decimal_number, terminator: "");
        print("\n Destination Base : ", destination_base, terminator: "");
        var flag: Bool = false;
        if (decimal_number < 0)
        {
            decimal_number = -decimal_number;
            flag = true;
        }
        //This is used to store result
        var result: String = "";
        //Transform decimal to other base
        while (decimal_number > 0)
        {
            result = (self.actual_value(decimal_number % destination_base)) + result;
            decimal_number = decimal_number / destination_base;
        }
        if (flag == true)
        {
            result = "-" + result;
        }
        //Display result
        print("\n Result : ", result ,"\n", terminator: "");
    }
}
func main()
{

    let obj: NumberConversion = NumberConversion();
    //Test case
    
    obj.decimal_to_other(55, 2);
    obj.decimal_to_other(125, 16);
    obj.decimal_to_other(125, 8);
    obj.decimal_to_other(-28, 16);
    obj.decimal_to_other(24, 4);
    obj.decimal_to_other(2124, 16);

}
main();

Output

 Decimal Number   :  55
 Destination Base :  2
 Result :  110111

 Decimal Number   :  125
 Destination Base :  16
 Result :  7D

 Decimal Number   :  125
 Destination Base :  8
 Result :  175

 Decimal Number   :  -28
 Destination Base :  16
 Result :  -1C

 Decimal Number   :  24
 Destination Base :  4
 Result :  120

 Decimal Number   :  2124
 Destination Base :  16
 Result :  84C


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