# Number system conversion

Number system conversion is a fundamental concept in computer science and mathematics. Different number systems are used to represent numerical values, and it is crucial to be able to convert numbers from one base to another. The most commonly used number systems include decimal (base-10), binary (base-2), octal (base-8), and hexadecimal (base-16).

In this article, we will explore the problem of converting numbers from one base to another. The task is to create a program that takes a source number and its base, and then converts it to the desired destination base. The code provided is a Java program that demonstrates number system conversion for decimal, octal, binary, and hexadecimal bases.

## Problem Statement

The problem can be summarized as follows: Given a source number and its base, convert the number to the desired destination base. The source number can be in any base, and the destination base can also be any base. The code provided defines a class called `NumberConversion`, which contains methods to convert numbers from one base to another.

## Explanation with Example

To understand the concept of number system conversion, let's take an example. Suppose we have a decimal number 255, and we want to convert it to its binary representation. Here, the source number is 255, and the source base is 10 (decimal), and the destination base is 2 (binary).

Step 1: If the source base is not decimal (base-10), we need to first convert the source number to decimal. In our example, the source base is decimal, so we skip this step.

Step 2: Now, we convert the decimal number 255 to binary. To do this, we continuously divide the decimal number by 2, and the remainders obtained at each step will form the binary representation.

``````Step 1: 255 ÷ 2 = 127 with a remainder of 1 (LSB)
Step 2: 127 ÷ 2 = 63 with a remainder of 1
Step 3: 63 ÷ 2 = 31 with a remainder of 1
Step 4: 31 ÷ 2 = 15 with a remainder of 1
Step 5: 15 ÷ 2 = 7 with a remainder of 1
Step 6: 7 ÷ 2 = 3 with a remainder of 1
Step 7: 3 ÷ 2 = 1 with a remainder of 1
Step 8: 1 ÷ 2 = 0 with a remainder of 1 (MSB)
``````

Step 3: Now, we read the remainders from the last step in reverse order, and that gives us the binary representation of 255, which is `11111111`.

## Pseudocode

Before explaining the algorithm, let's first provide a pseudocode representation of the number system conversion algorithm:

``````function actual_value(num):
if num >= 0 and num <= 9:
return char(num + '0')
else:
return char(num - 10 + 'A')

function one_to_other(source_number, source_base, destination_base):
decimal = 0
if source_base != 10:
multiplier = 1
while source_number != 0:
decimal += (source_number % 10) * multiplier
multiplier *= source_base
source_number /= 10

result = ""
while decimal > 0:
result = actual_value(decimal % destination_base) + result
decimal /= destination_base

return result
``````

## Algorithm Explanation

The algorithm for number system conversion follows these steps:

Step 1: Define a function `actual_value(num)` that takes a number `num` as input and returns its corresponding character representation. For numbers from 0 to 9, it returns their characters '0' to '9', and for numbers from 10 to 15, it returns characters 'A' to 'F'.

Step 2: Define a function `one_to_other(source_number, source_base, destination_base)` that performs the number conversion. It takes the `source_number` to be converted, its `source_base`, and the `destination_base` as inputs.

Step 3: Check if the `source_base` is not decimal (base-10). If so, convert the `source_number` to its decimal representation. This is done by continuously dividing the `source_number` by 10 and accumulating the remainders multiplied by the appropriate multiplier. The multiplier is incremented by multiplying it with the `source_base` at each iteration.

Step 4: Once we have the `decimal` value of the `source_number`, we convert it to the `destination_base`. This is achieved by repeatedly dividing the `decimal` by the `destination_base` and storing the remainders. The remainders will be the digits of the converted number in the `destination_base`.

Step 5: While converting the `decimal` to the `destination_base`, we use the `actual_value()` function to get the corresponding character representation of each digit. This ensures that we handle numbers greater than 9 using alphabets in hexadecimal representation.

Step 6: The converted number in the `destination_base` is obtained by reading the remainders from the last step in reverse order.

## Code Solution

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``````

## Resultant Output Explanation

The Java program provided in the code section demonstrates the number system conversion algorithm. It performs conversions between decimal, octal, binary, and hexadecimal bases and displays the results.

Let's go through the output for each test case:

1. Convert a decimal 16 to octal (10 to 8): Source Number: 16 (Decimal) Source Base: 10 (Decimal) Destination Base: 8 (Octal) Result: 20 (Octal)

2. Convert an octal 21 to decimal (8 to 10): Source Number: 21 (Octal) Source Base: 8 (Octal) Destination Base: 10 (Decimal) Result: 17 (Decimal)

3. Convert a decimal 122 to hexadecimal (10 to 16): Source Number: 122 (Decimal) Source Base: 10 (Decimal) Destination Base: 16 (Hexadecimal) Result: 7A (Hexadecimal)

4. Convert an octal 32 to binary (8 to 2): Source Number: 32 (Octal) Source Base: 8 (Octal) Destination Base: 2 (Binary) Result: 11010 (Binary)

5. Convert a hexadecimal 20 to binary (16 to 2): Source Number: 20 (Hexadecimal) Source Base: 16 (Hexadecimal) Destination Base: 2 (Binary) Result: 100000 (Binary)

6. Convert a base 5 (32) to base 2 (5 to 2): Source Number: 34 (Base 5) Source Base: 5 (Base 5) Destination Base: 2 (Binary) Result: 10011 (Binary)

## Time Complexity of the Code

The time complexity of the given code can be analyzed based on the operations performed in the main function `one_to_other` and the auxiliary function `actual_value`. Let's break down the time complexity of each part:

1. Conversion from non-decimal source base to decimal: In the provided code, this conversion occurs within the while loop, where the source number is continuously divided by 10 (source_base) until it becomes 0. In each iteration, basic arithmetic operations (modulus and division) are performed, but the number of iterations depends on the number of digits in the `source_number`, which we can denote as `n`.

Time Complexity: O(n)

2. Conversion from decimal to the destination base: The second while loop in the code converts the decimal value to the destination base. Similar to the previous conversion, it runs until the `decimal` becomes 0, and in each iteration, basic arithmetic operations (modulus and division) are performed. The number of iterations depends on the number of digits in the converted number, which we can denote as `m`.

Time Complexity: O(m)

3. Actual Value Function (auxiliary function): The `actual_value` function performs a simple if-else check and returns a character value based on the input number. This function has a constant time complexity as it only involves comparisons and simple arithmetic operations.

Time Complexity: O(1)

4. Main Function: The main function calls the `one_to_other` method for each test case. Each test case involves the two conversions mentioned above. Since the conversions are performed sequentially, the overall time complexity is determined by the sum of the time complexities of the two conversions (non-decimal to decimal and decimal to destination base).

Overall Time Complexity for Main Function: O(n + m)

Therefore, the time complexity of the given code is O(n + m), where `n` is the number of digits in the source number and `m` is the number of digits in the converted number.

It's essential to note that in most practical cases, the number of digits `n` and `m` is related to the magnitude of the input numbers. As a result, the time complexity can often be represented as O(log x), where `x` is the value of the input or output number. This logarithmic relationship is typical for number system conversion algorithms, where the number of digits grows logarithmically with the magnitude of the numbers involved.

## Comment

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.